@ 2016-06-22T14:03:10Z <?php
class AreAnagrams {
public static function areStringsAnagrams($a, $b) {
$a_arr = str_split(strtolower($a));
$b_arr = str_split(strtolower($b));
$az = range('a', 'z');
foreach ($a_arr as $char) {
if (!isset($comp_arr[$char])) {
$comp_arr[$char] = 0;
}
if (in_array($char, $az)) {
$comp_arr[$char]++;
}
}
foreach ($b_arr as $b_char) {
if (!isset($comp_arr[$b_char])) {
return FALSE;
}
if (in_array($char, $az)) {
$comp_arr[$b_char]--;
}
}
if (array_sum($comp_arr) !== 0) {
return FALSE;
}
return TRUE;
}
}
echo AreAnagrams::areStringsAnagrams('neural', 'unreal') ? 'True' : 'False';
echo AreAnagrams::areStringsAnagrams('sample', 'Maples') ? 'True' : 'False';
echo AreAnagrams::areStringsAnagrams('better', 'brttee') ? 'True' : 'False';
echo AreAnagrams::areStringsAnagrams('letter to editor', 'Retorted Toilet') ? 'True' : 'False';
echo AreAnagrams::areStringsAnagrams('letter to editor', 'Delete Riot Trot') ? 'True' : 'False';
echo AreAnagrams::areStringsAnagrams('letter to editor', 'Loitered Totter') ? 'True' : 'False';
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.5.0 - 5.5.36 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 TrueTrueTrueFalseFalseFalse Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
TrueTrueTrueFalseFalseFalse preferences:dark mode live preview
177.59 ms | 402 KiB | 243 Q