Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Generate string

Lavrenty Lalas
Greenhorn
Posts: 1
Ok let`s say we have an array string like this:
String[] elements = {"a", "b", "c",...};

All I want is to generate all possible combinations (including also "aa...a","aa...b", etc).
I also run the code posted here here by Sai Narasimha Reddy but the combinations like aa, bb, .....ccb, ccc are not generated.

I tried another permutations and combinations that I found but none of them don`t generate all combinations (such as "aa...a","aa...b",...)

fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
first, the terms 'permutations' and 'combinations' are well defined terms, and are mutually exclusive. or I guess technically, the set of combinations is a subset of permutations (unless i got that backwards).

So, before we can do much else, tell us what you really want. Combinations are where the order doesn't matter, so "ABC" is considered the same thing as "CBA".

Next...do you have to use all the elements each time? if i have 'a', 'b' and 'c', do you want 'ab', 'ac' and 'bc'? (and 'ba', 'ca and 'cb')?

finally, I'll tell you what i would tell anyone.

Before you write a single line of code, write down on paper, in english, how YOU would do this by hand, step-by-step. Then go back and revise each step, making it more and more detailed. Once you do that, writing the code is orders of magnitude easier.

Rob Spoor
Sheriff
Posts: 20527
54
You say you want "aa...a". I take it that's virtually an infinite number of "a"s. That means that the total number of combinations is just as infinite, and it's going to be impossible to generate them all. After all, once you get "aa...a" of size X, you can get another combination by adding another "a", getting "aa...a" of size X + 1.

Campbell Ritchie
Sheriff
Posts: 48953
60
... and welcome to the Ranch