How to assign values to letters to create unique values per word when all letters are added together?

1.5k Views Asked by At

I'm writing a program to match anagrams in order to practice coding. One way I want to try this is to assign values to letters such that adding up the letters in the individual words creates a unique value. This will allow me to match words without worrying about the order of the letters. I think I can do this by playing around with different approaches but I really want to know how to do this mathematically. Assume all letters will be uppercase (so 26 total letters) and, for this example, I don't want to worry about any kind of character other than A-Z.

2

There are 2 best solutions below

4
On

This will require large numbers, as you get $26^n$ possibilities for an $n$ lettered word. To make a unique number for each word, have the letters numbered 0 to n-1, and find the sum

unique word number=$\sum_{i=0}^{n-1}$$26^i$*letter value of letter i(0 to 25, not 1 to 26)

This will make every number unique for every word, even if they end up large.

0
On

Every letter is a different prime number, multiply them all together for a word. MAY and YAM should result in the same value.