Last active
October 13, 2019 23:29
-
-
Save mohamedhafezqo/44c3fac93305418b875adb5807c136da to your computer and use it in GitHub Desktop.
Anagram functions
This file contains hidden or 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 | |
// 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