0

I am looking for help with an algorithm that will generate all the possible combinations of n-random letters in decreasing length. For example, the array of 'a','b','c' should generate:

abc acb bac bca cab cba ab ac ba bc ca cb a b c

where letters cannot repeat themselves once used

  • 2
    Please make an honest attempt at the problem before asking here. – Ken Y-N Dec 19 '14 at 07:05
  • Before trying any code please study permutation and combination. It will help you lot. – Vipul Hadiya Dec 19 '14 at 07:07
  • I've looked through NPM for any solutions and none of them handle a situation that produces combinations in decreasing length. I've attempted my own, but nothing efficient comes to mind, besides potentially removing the first element in an array, regenerating combinations and then removing the first again... – OaklandFanatic Dec 19 '14 at 07:07
  • @VipulHadiya I've have, but neither deals with decreasing lengths of permutations for every possible subset of letters – OaklandFanatic Dec 19 '14 at 07:09
  • Can you generate subsets out of the whole set? Yes. Can you write a for loop that goes from `n` to `1` in decreasing order? Yes. All that remains is generating the permutations. – Luchian Grigore Dec 19 '14 at 07:13
  • If you want to cut to the chase: [*https://gist.github.com/axelpale/3118596*](https://gist.github.com/axelpale/3118596). Also see answers at [*Javascript, all possible sums of an array members (up to 4)*](http://stackoverflow.com/questions/27557888/javascript-all-possible-sums-of-an-array-members-up-to-4/27558192#comment43546411_27558192) – RobG Dec 19 '14 at 08:31

1 Answers1

1

"Permutation with decresing length" is basically just a loop of standard permutation task.:

  • you are given a set of n letters
  • take each letter and add it to the output
  • take all possible pairs of letters and add them to the output
  • take all possible triplets of letters and add them to the output
  • ... do until you reach N
naivists
  • 32,681
  • 5
  • 61
  • 85