Skip to content

Instantly share code, notes, and snippets.

@mohamedhafezqo
Last active October 13, 2019 23:29
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 mohamedhafezqo/44c3fac93305418b875adb5807c136da to your computer and use it in GitHub Desktop.
Save mohamedhafezqo/44c3fac93305418b875adb5807c136da to your computer and use it in GitHub Desktop.
Anagram functions
<?php
// Using built in metod `count_chars`
function isAnagram(string $a, string $b): bool
{
return count_chars(strtolower($a), 1) == count_chars(strtolower($b), 1);
}
// Using array_diff expected $a or $b consists from [a-z]
function isAnagram(string $a, string $b): bool
{
$a = str_split(strtolower($a));
$b = str_split(strtolower($b));
if (count($a)!= count($b) ) {
return false;
}
if (count(array_diff($a ,$b)) > 0 ) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment