Skip to content

Instantly share code, notes, and snippets.

@wtorsi
Last active March 21, 2020 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtorsi/2d4735aff677bd0c3c9ff425935391db to your computer and use it in GitHub Desktop.
Save wtorsi/2d4735aff677bd0c3c9ff425935391db to your computer and use it in GitHub Desktop.
ASCII anagrams
<?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