-1

Given a number, say n = 201

S = { 2, 0, 1, 20, 201, 21}

I know this is not a rare question, but I haven't found the exact keywords including "permutations" "combinations" "[title]" etc

Any keywords/sources are also appreciated so I can research by myself!

Gunasekar
  • 611
  • 1
  • 8
  • 21
Vu Le
  • 3
  • 3
  • 2
    Something like this? https://stackoverflow.com/questions/11774059/generate-subsequences – m69's been on strike for years Sep 19 '18 at 03:09
  • 1
    Thank you! So the keyword must be "subsequence" To further address this question, for n = 201 S = { 2 , 0 , 1 , 20 , 21, 12 , 10, 201 } In this case, what would be the keywords? – Vu Le Sep 19 '18 at 18:58
  • @VuLe , why don’t you simply start by splitting your number into single digits and generate the power set? You could then generate all permutations of each result to get your desired answer. – Joseph Wood Sep 20 '18 at 09:49

1 Answers1

0

Here's a recursive Java solution that uses math operations to generate all permutations of deleted digits:

static void permuteDeletions(List<Integer> perms, int n)
{
  if(n >= 10) 
  {
    permuteDeletions(perms, n/10);
    for(int p, i=perms.size()-1; i>=0; i--)
      if((p = perms.get(i)) > 0) perms.add(10*p + n%10);
  }   
  perms.add(n%10);
}

Test:

public static void main(String[] args)
{
  List<Integer> perms = new ArrayList<>();
  permuteDeletions(perms, 201);
  Collections.sort(perms);
  System.out.println(perms);    
}

Output:

[0, 1, 2, 20, 21, 201]
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16