|
|
|
|
|
by _pastel
2798 days ago
|
|
I have a pet implementation of the frequency map that I'm overly fond of, for ascii strings: (1) keep an array of length 127 that you re-use and set to 0 between calls
(2) for each character in the first string, increment the array at the character's index
(3) for each character in the second string, decrement the array at the character's index
If you end up with all 0s, it's an anagram. |
|
This way you only need to check the character values you actually use, and not all 127. It also generalizes trivially to larger character sets.