4 ms·
Solution 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 comp
by sesm 11y ago
Solution 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.