File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -266,6 +266,31 @@ var canConstruct = function(ransomNote, magazine) {
266
266
};
267
267
```
268
268
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
+ ```
269
294
270
295
271
296
-----------------------
You can’t perform that action at this time.
0 commit comments