# Combination of characters.

• February 28th, 2013, 01:36 AM
BlitzBerg
Combination of characters.
Well suppose I got a String "ABC", I need to put up all possible combinations.

Eg. AAA, AAB, AAC, ABA, ABB.....etc

I need a non-recursive technique, and the code should work for Strings of length less than 30.

Anybody with a solution?
• February 28th, 2013, 10:04 AM
Mur16
Re: Combination of characters.
What do you have so far?
• March 4th, 2013, 02:05 AM
Niya
Re: Combination of characters.
Hmm...well its kinda like counting don't you think ? Imagine we only had 4 symbols for representing basic numbers:-
Code:

```000 001 002 003 010 011 012 013 100 101 102 ........```
Try implementing an algorithm based on that.
• March 4th, 2013, 11:48 AM
Mur16
Re: Combination of characters.
This is kool without recursion. There are actually N^N posibilites. I would create that many arrays of char with length N (a 1-dim array of string would probably work too with appending). Then you need to loop through your input string. Each letter will be repeated the same number of times as N raised to the output position-1.

input: abc (N=3, 27 possible words)

1 2 3 <-position
1 3 9 <-repeats N^(position-1)

a a a
b a a
c a a
a b a
b b a
c b a
a c a
b c a
c c a
a a b
b a b
c a b
a b b
b b b
c b b
a c b
b c b
c c b
a a c
b a c
c a c
a b c
b b c
c b c
a c c
b c c
c c c

So you need two loops. One to itterate through the characters and the other to repeat the character depending on it's position in the output.