An anagram is word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word anagram can be rearranged into "nag a ram".
4 Ways to Code an Anagram
- Most efficient solution in O(n) time complexity: Create two dictionaries, loop through each dictionary and get the count for each letter, compare the dictionaries to see if a letter is inside the other
- Checking off each letter in word 1 to see if it appears in word 2
- Sort each word alphabetically and compare words
- Count each letter and compare