I realize this was posted a few years ago and I don’t know where you are currently at in your computer science journey. Congratulations on coming up with an algorithm to solve your assignment. You mentioned how the time complexity of your algorithm was fairly poor in that as the size of the word increased the time it took to solve increased in a non linear manner (much slower actually).

I think it’s always good idea to think about these problems a bit before you solve them. The first question would obviously be what is an anagram and how do anagram relate. As you know the relationship between two anagrams is such that you can form the second word by rearranging the letters in the first.

Naturally when you first look at the problem it seems like an obvious solution is to find all possible combinations of letters for a given word. You can then compare this against a dictionary and return the matches.

However let’s think about this for a second. That solution might be fine for two letter words because there is only two possible combinations. And it’s probably ok for three letter words with 6 combinations. I would say it’s ok for four and five too. But can you tell me how many combinations there are for 6, 7, 8, 9, and 10 letter words?

Let’s take a step back and think about what an anagram really is by looking at the difference between words that are not anagrams as oppose to the similarities between anagrams. If you had to describe to me an anagram by what it is not or how it is dissimilar to other words what would you say?

Well an anagram is not any word of different lengths, correct? And any word that contains any letter that does not appear in the first word is also not an anagram of that word. Also two words are not anagrams if the frequency of any letter used in word 1 is different than the frequency used in word 2.

Two things pop out to me after breaking this problem down. Firstly, it seems to me that anagrams are somewhat unique to each other and different from all others. And what is our goal here again. We want to know if one word is an anagram of another word. IE we want to know if two words are the same in a particular way with each other and different from others based of a particular property. Let’s start to think mathematically here and instead of looking at a word as a string of characters let’s look at them as a number. Let’s take a three character word as an example for simplicity. Let’s think of the letters in this word in terms of numbers. Is there any way to assign a number to every letter of the alphabet such that if you do an operation (such as multiple, subtract, divide, etc) on all the letters you will arrive at a total that is the same for all anagrams and different for non anagrams? Well what do you know about prime numbers? A prime number is a number that only has one pair of multiples, correct? What about prime factorization? Let’s take the word “abc”. Let’s assign a=1, b=3, c=5. Multiples together is 15. Can you think of any other 3 letter word that would multiple to be 15 if you assign each letter to a sequential prime number. Well, acb, bac, bca, cba, and cab obviously. But is there any else? No? What did we just do there? If we wanted to tell if a word is an anagram, could we compare words that way?

I think we can, but I also want to take another step back. Before we said an anagram has a few properties. It always has the same length, it always has the same letters, and it always has the same frequency of letters (if word 1 has 2 a and 3 b so must word 2). Well which one of those properties makes the others redundant? Does two words of the same length always have the same letters? Do two words with same letters always have the same frequency? No? But do two words that have the same frequency of every letter always have the same length and the same letters? Yes? So maybe we are determining anagrams we can compare the histogram of two words based on letter frequency

]]>