diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 64503cef..32b0ff7f 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -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; + } +} +``` -----------------------