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?
Printable View
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?
What do you have so far?
Hmm...well its kinda like counting don't you think ? Imagine we only had 4 symbols for representing basic numbers:-
Try implementing an algorithm based on that.Code:000
001
002
003
010
011
012
013
100
101
102
........
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.