Skip to content

Commit 68710ed

Browse files
committed
添加 0242.有效的字母异位词 PHP版本
1 parent b4c6130 commit 68710ed

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

problems/0242.有效的字母异位词.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,41 @@ func isAnagram(_ s: String, _ t: String) -> Bool {
221221
}
222222
```
223223

224+
PHP:
225+
```php
226+
class Solution {
227+
/**
228+
* @param String $s
229+
* @param String $t
230+
* @return Boolean
231+
*/
232+
function isAnagram($s, $t) {
233+
if (strlen($s) != strlen($t)) {
234+
return false;
235+
}
236+
$table = [];
237+
for ($i = 0; $i < strlen($s); $i++) {
238+
if (!isset($table[$s[$i]])) {
239+
$table[$s[$i]] = 1;
240+
} else {
241+
$table[$s[$i]]++;
242+
}
243+
if (!isset($table[$t[$i]])) {
244+
$table[$t[$i]] = -1;
245+
} else {
246+
$table[$t[$i]]--;
247+
}
248+
}
249+
foreach ($table as $record) {
250+
if ($record != 0) {
251+
return false;
252+
}
253+
}
254+
return true;
255+
}
256+
}
257+
```
258+
224259
## 相关题目
225260

226261
* 383.赎金信

0 commit comments

Comments
 (0)