# Every combination possible algorithm

Printable View

• 06-30-2006
Kate
Every combination possible algorithm
Hi,

I am trying to figure out a way to store every combination of letters in an array using C.

For example, If i have the word - COMPUTER

I want to store the following in an array -

COMPUTRE
COMPURET
COMPUETR
...
ect, ect for all 40000 odd combinations.

No idea where to start :)

Any help would be great.

Thanks :)
• 06-30-2006
jEssYcAt
Start writing down the combinations on paper or in a notepad (like you did for your example) and take note of the steps you are going through. An example might be:

Step 1 Write the original word.
Step 2 Pick the last letter of the word
Step 3 Shift the letter to the left
Step 4 Write the new variation
Step 5 Repeat steps 3 and 4 until the letter you are shifting is all the way to the left.
Step 6 Repeat steps 3, 4 and 5 using the new last letter until there are no more variations.

Once you work out the steps you can write a program that follows those steps.

My example is not complete (just a quick example off the top of my head), but if you can figure out why it is not complete you can work on revising the steps to make it complete, or start over and try a different approach.
• 06-30-2006
jafet
I imagine you'd sort all the letters first and check for repeat letters, then handle them seperately.
• 06-30-2006
fgw_three
Start with a simple case, say a two letter word, and do what one of the other posters said about doing it by hand and noticing the steps you are taking.

Pick a three letter word and do the same -- see if any patter occurs in the steps you are taking.

Finally, try to generalize the steps you are taking.

Once you get that figured out -- translate it into C code.
• 06-30-2006
Happy_Reaper
I see a recursive solution to this quickly develloping (although I'm not quite sure if it's feasible on a computer...)
• 06-30-2006
Tonto
Google: Permutations.