<?php function isAnagram($satu, $dua) { $satuAr = str_split($satu); $duaAr = str_split($dua); $jumlah = 0; foreach ($satuAr as $k => $v) { if (!in_array($v, $duaAr)) { $jumlah++; } } if ($jumlah != 0) { echo "false"; } else { echo "true"; } } #isAnagram("secure","resc ue"); isAnagram("conifers", "fir cones"); #isAnagram("google","facebook");
<?php $wordPairs[] = array("cat", "act"); // happy path $wordPairs[] = array("cat", "apt"); // not so happy path $wordPairs[] = array("cat", "pact"); // not so happy path $wordPairs[] = array("cat", "tact"); // not so happy path $wordPairs[] = array("31121984", "12311984"); // happy path foreach ($wordPairs as $words) { if (isAnagram($words[0], $words[1])) { echo "{$words['0']}, {$words['1']} are anagrams. \n"; } else { echo "{$words['0']}, {$words['1']} are not anagrams. \n"; } } function isAnagram($word1, $word2) { if (empty($word1) || empty($word1) || strlen($word1) != strlen($word2)) { // check for empty strings or if // one of the words atleast has one or more chars more than the other return false; } else { $charFreqMapWord1 = __buildFrequencyMap($word1); print_r($charFreqMapWord1); $charFreqMapWord2 = __buildFrequencyMap($word2); print_r($charFreqMapWord2); foreach ($charFreqMapWord1 as $char => $count) {