3 ms·
That anagram question...I would have never figured out to use sorting. I would have done a simple loop that checks to see if the letter exists in the other word
by fallat 11y ago
That anagram question...I would have never figured out to use sorting. I would have done a simple loop that checks to see if the letter exists in the other word. Loop until false is returned...The length check seems 100% necessary to solve this problem.
- sesm 11y agoSolution they've described doesn't account for duplicate letters in a word. The right solution would be to build frequency maps of letters in each word and compare the maps. (defn anagram? [a b] (= (frequencies a) (frequencies b)))
- fallat 11y agoNo, it does. Once both strings are sorted alphabetically, you just compare them. "tallaf" -> sort -> "aafllt" "laftal" -> sort -> "aafllt" Does "aafllt" == "aafllt" ? yes? Then we're good! What you're saying: "tallaffffff" -> sort -> "aaffffffllt" "latfal" -> sort -> "aafllt" Does "aaffffffllt" == "aafllt" ? Of course not. The length check is just early exit. Actually for my solution the length is necessary. Your solution is good too - I actually googled after to see other possible solutions and counting characters was the "second" solution.
- NLips 11y agoYour method doesn't account for the following case: are_anagrams("moo", "mom") Each letter in "moo" appears in "mom", and the length is the same, but the words are not anagrams of each other.
- fallat 11y agoHaha, nice catch. So there appears to be really only two solutions to the problem.