添加 0242.有效的字母异位词 PHP版本

This commit is contained in:
nolanzzz
2021-09-02 23:47:00 -04:00
parent b4c6130347
commit 68710ed006

View File

@ -221,6 +221,41 @@ func isAnagram(_ s: String, _ t: String) -> Bool {
}
```
PHP
```php
class Solution {
/**
* @param String $s
* @param String $t
* @return Boolean
*/
function isAnagram($s, $t) {
if (strlen($s) != strlen($t)) {
return false;
}
$table = [];
for ($i = 0; $i < strlen($s); $i++) {
if (!isset($table[$s[$i]])) {
$table[$s[$i]] = 1;
} else {
$table[$s[$i]]++;
}
if (!isset($table[$t[$i]])) {
$table[$t[$i]] = -1;
} else {
$table[$t[$i]]--;
}
}
foreach ($table as $record) {
if ($record != 0) {
return false;
}
}
return true;
}
}
```
## 相关题目
* 383.赎金信