Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm Issue: letter combinations

I'm trying to write a piece of code that will do the following:

Take the numbers 0 to 9 and assign one or more letters to this number. For example:

0 = N,
1 = L,
2 = T,
3 = D,
4 = R,
5 = V or F,
6 = B or P,
7 = Z,
8 = H or CH or J,
9 = G

When I have a code like 0123, it's an easy job to encode it. It will obviously make up the code NLTD. When a number like 5,6 or 8 is introduced, things get different. A number like 051 would result in more than one possibility:

NVL and NFL

It should be obvious that this gets even "worse" with longer numbers that include several digits like 5,6 or 8.

Being pretty bad at mathematics, I have not yet been able to come up with a decent solution that will allow me to feed the program a bunch of numbers and have it spit out all the possible letter combinations. So I'd love some help with it, 'cause I can't seem to figure it out. Dug up some information about permutations and combinations, but no luck.

Thanks for any suggestions/clues. The language I need to write the code in is PHP, but any general hints would be highly appreciated.

Update:

Some more background: (and thanks a lot for the quick responses!)

The idea behind my question is to build a script that will help people to easily convert numbers they want to remember to words that are far more easily remembered. This is sometimes referred to as "pseudo-numerology".

I want the script to give me all the possible combinations that are then held against a database of stripped words. These stripped words just come from a dictionary and have all the letters I mentioned in my question stripped out of them. That way, the number to be encoded can usually easily be related to a one or more database records. And when that happens, you end up with a list of words that you can use to remember the number you wanted to remember.

like image 954
Michiel Avatar asked Sep 19 '08 14:09

Michiel


People also ask

How many combinations of letters are possible?

This gives that the number of combinations that are possible with the alphabet, or 26 letters, without repetition is 67,108,863.

How many combinations of 2 letter codes are there?

There are 325 possible combinations with two letters. To determine this number of combinations, we use the fact that the alphabet has 26 letters. Therefore, we want to know the number of possible combinations of 2 letters taken from 26 letters. Thus, we can use our formula with n = 26 and r = 2.

How many combinations are in a 4 letter code?

The number of possible combinations that are possible with 4 letters is 14,950. The alphabet contains 26 letters total, and we want to know how many combinations of 4 letters we can make from those 26 letters.

How many possible 3 letter combos are there?

26⋅26⋅26=263=17576. If you want the letters to be unique, the calculation changes slightly.


1 Answers

It can be done easily recursively.

The idea is that to handle the whole code of size n, you must handle first the n - 1 digits. Once you have all answers for n-1 digits, the answers for the whole are deduced by appending to them the correct(s) char(s) for the last one.

like image 84
David Pierre Avatar answered Sep 21 '22 21:09

David Pierre