-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path242.php
More file actions
32 lines (25 loc) · 749 Bytes
/
242.php
File metadata and controls
32 lines (25 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
class Solution
{
public function isAnagram(string $s, string $t): bool
{
$len = strlen($s);
if ($len !== strlen($t)) {
return false;
}
$letters1 = [];
$letters2 = [];
for ($i = 0; $i < $len; $i++) {
$ch1 = $s[$i];
$ch2 = $t[$i];
$letters1[$ch1] = array_key_exists($ch1, $letters1) ? $letters1[$ch1] + 1 : 1;
$letters2[$ch2] = array_key_exists($ch2, $letters2) ? $letters2[$ch2] + 1 : 1;
}
foreach ($letters1 as $letter => $reps) {
if (! array_key_exists($letter, $letters2) || $reps !== $letters2[$letter]) {
return false;
}
}
return true;
}
}