0

I have a Hashmap called H1.

There are n Hashmap keys for H1.

For H1 hashmap, the program will create all the permutations of the powerset {1,2,3,4,...n}.

So in other words, if n = 5, any number from 1,2,3,..5555 is a valid list for H1.

So if,

Key 1 = 22

Key 2 = 50

Key 3 = 12

Key 4 = 44

Key 5 = 55

For 111 = {22,22,22}, for 213 = {50,22,12} for 12345 = {22,50, 12, 44, 55}.

I essentially need to find all of the lists in all possible combinations, in every order (i.e.: 1342 != 3142).

I have a possible solution, but I do not find it optimal at all, it involves converting int values to string and looking at each character element in the string, I am looking to see if anyone knows of a more efficient way.

varsha
  • 1,620
  • 1
  • 16
  • 29
user3256369
  • 135
  • 1
  • 9
  • 2
    Without seeing your possible solution it's impossible to evaluate how efficient it is. Note that the minimal possible time it takes to create a powerset should be O(2^n), because that is the number of elements you have to create: almost no algorithms are going to be 'fast'. – Nathaniel Ford Mar 07 '15 at 05:47

1 Answers1

1

Algorithm to generate all possible permutations of a list?

It's a complicated algorithm any way you want to do it, I think recursively is the fun way.

The recursive way basically removes one element, calls the recursion on the shortened list, then returns the recursive result with the removed element at each position.

Community
  • 1
  • 1
Will
  • 4,299
  • 5
  • 32
  • 50