This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generate string  RSS feed

 
Lavrenty Lalas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12527
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 55329
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!