Last active
March 21, 2020 06:35
-
-
Save wtorsi/2d4735aff677bd0c3c9ff425935391db to your computer and use it in GitHub Desktop.
ASCII anagrams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function map(string $a): array | |
{ | |
$len = \strlen($a); | |
$map = new SplFixedArray(127); | |
for ($i = 0; $i < $len; ++$i) { | |
$code = \ord($a[$i]); | |
$map[$code] = ($map[$code] ?? 0) + 1; | |
} | |
return $map->toArray(); | |
} | |
function findASCIIAnagram(string $a, string $b): bool | |
{ | |
return map($a) === map($b); | |
} | |
echo findASCIIAnagram('CABAC', 'ABCAC') ? '+' : '-'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment