Skip to content

Commit

Permalink
Generate subsets without duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Atousa committed May 20, 2021
1 parent 57bd07b commit 64cf1fb
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Recursion/SubsetsWithDuplicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//leetcode 90
public class SubsetsWithDuplicate {

static List<List<Integer>> result = new ArrayList<>();

public static List<List<Integer>> subsetsWithDuplicate(int[] s) {
Arrays.sort(s);
List<Integer> slate = new ArrayList<>();
subsetsWithDuplicateHelper(s, 0, slate);
return result;
}


private static void subsetsWithDuplicateHelper(int[] s, int idx, List<Integer> slate) {

if (idx == s.length) {
result.add(new ArrayList<>(slate));
return;
}
int counter = 1;
while (idx < s.length-1 && s[idx] == s[idx + 1]) {
counter++;
idx++;
}
int c = counter;
subsetsWithDuplicateHelper(s, idx + 1, slate);
while (c > 0) {
slate.add(s[idx]);
subsetsWithDuplicateHelper(s, idx + 1, slate);
c--;
}
while(counter>0) {
slate.remove(slate.size()-1);
counter--;
}
}

public static void main(String[] args) {
int[] c = {1,2,1,1,2};
subsetsWithDuplicate(c);
System.out.println(result);
}
}

0 comments on commit 64cf1fb

Please sign in to comment.