0

I need to get all the combination of an array. From the shortest combo (1 letter) to the longest combinations (|number of letters|) without duplication

For example:

ArrayList<String> array = new ArrayList<String>();
array.add("a"); 
array.add("d"); 
array.add("e");
IterationWithoutDuplication(array);

public void IterationWithoutDuplication(array){
  //some kind of a loop/recursion
  printCurrentCombo();
  //do some other work on the specific combo
}

Output should be:

a
d
e
ad
ae
da
de
ea
ed
ade
aed
dae
dea
ead
eda

Notice how it goes first on:

  • 1 letter combinations; then
  • 2 letters combinations; then
  • 3 letters combinations; then
  • ..
  • |array size| letters combinations

But all the combinations are unique and doesnt contain the same letter more then once.

Of course I dont wish just to print it, but rather do some work - the question is how to scan an array this way efficient.

Nir
  • 2,497
  • 9
  • 42
  • 71
  • check following question: http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n – sodik Sep 09 '13 at 20:11
  • I'm not looking to read a whole book for this. The other answers there not quite answer mine. – Nir Sep 09 '13 at 20:52
  • loops inside each other, i'll try to find one, this reminds me with my first task :D wait a minute, i dont get your formula !!! what is your sorting formula for your letters, or you want random letters to be printed or what ! – Ahmed Adel Ismail Sep 09 '13 at 20:54
  • so do you just want all the combinations of letters? and do work on each combination? – Adrian Sep 10 '13 at 13:34
  • I've edit the question, and voted to reopen it. If you vote for off-topic - please comment here so I'll know what I need to fix! – Nir Sep 11 '13 at 10:13

0 Answers0