[leetcode][PHP实现]Valid Anagram

 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
33
34
35
<?php
class Solution
{
    public function isAnagram($s, $t)
    {
        $s = strtolower($s);
        $t = strtolower($t);
        for ($i = 0; $i < 26; $i++)
        {
            $sChars[$i] = 0;
            $tChars[$i] = 0;
        }
        for ($i = 0; $i < strlen($s); $i++)
        {
            $sChars[ord($s[$i]) - ord('a')]++;
        }
        for ($i = 0; $i < strlen($t); $i++)
        {
            $tChars[ord($t[$i]) - ord('a')]++;
        }
        //var_dump($sChars);
        //var_dump($tChars);
        for ($i = 0; $i < 26; $i++)
        {
            if ($sChars[$i] != $tChars[$i])
            {
                return false;
            }
        }
        return true;
    }
}
$solution = new Solution();
echo $solution->isAnagram("HelloWorld", "whelloorld");
?>
Licensed under CC BY-NC-SA 4.0