mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0242.有效的字母异位词 PHP版本
This commit is contained in:
@ -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.赎金信
|
* 383.赎金信
|
||||||
|
Reference in New Issue
Block a user