-
February 28th, 2013, 02:36 AM
#1
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, 11:04 AM
#2
Re: Combination of characters.
-
March 4th, 2013, 03:05 AM
#3
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, 12:48 PM
#4
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.
Last edited by Mur16; March 4th, 2013 at 02:12 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|