GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Generate string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generate string " Watch "Generate string " New topic
Author

Generate string

Lavrenty Lalas
Greenhorn

Joined: Jul 27, 2011
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",...)


Please help and thanks in advance.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11350
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
... and welcome to the Ranch
 
GeeCON Prague 2014
 
subject: Generate string