mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0383.赎金信 PHP版本
This commit is contained in:
@ -266,6 +266,31 @@ var canConstruct = function(ransomNote, magazine) {
|
||||
};
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param String $ransomNote
|
||||
* @param String $magazine
|
||||
* @return Boolean
|
||||
*/
|
||||
function canConstruct($ransomNote, $magazine) {
|
||||
if (count($ransomNote) > count($magazine)) {
|
||||
return false;
|
||||
}
|
||||
$map = [];
|
||||
for ($i = 0; $i < strlen($magazine); $i++) {
|
||||
$map[$magazine[$i]] = ($map[$magazine[$i]] ?? 0) + 1;
|
||||
}
|
||||
for ($i = 0; $i < strlen($ransomNote); $i++) {
|
||||
if (!isset($map[$ransomNote[$i]]) || --$map[$ransomNote[$i]] < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user