Skip to content

Commit 7f30649

Browse files
committed
添加 0383.赎金信 PHP版本
1 parent 80eb43f commit 7f30649

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

problems/0383.赎金信.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,31 @@ var canConstruct = function(ransomNote, magazine) {
266266
};
267267
```
268268

269+
PHP:
270+
```php
271+
class Solution {
272+
/**
273+
* @param String $ransomNote
274+
* @param String $magazine
275+
* @return Boolean
276+
*/
277+
function canConstruct($ransomNote, $magazine) {
278+
if (count($ransomNote) > count($magazine)) {
279+
return false;
280+
}
281+
$map = [];
282+
for ($i = 0; $i < strlen($magazine); $i++) {
283+
$map[$magazine[$i]] = ($map[$magazine[$i]] ?? 0) + 1;
284+
}
285+
for ($i = 0; $i < strlen($ransomNote); $i++) {
286+
if (!isset($map[$ransomNote[$i]]) || --$map[$ransomNote[$i]] < 0) {
287+
return false;
288+
}
289+
}
290+
return true;
291+
}
292+
}
293+
```
269294

270295

271296
-----------------------

0 commit comments

Comments
 (0)