Skip to content

Commit 0ca04e8

Browse files
Create 90-Subsets-II.java
1 parent 0d09cf3 commit 0ca04e8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

java/90-Subsets-II.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//Similar to subsets 1. Here, we'll just take care of the duplicates.
2+
//This video was helpful https://www.youtube.com/watch?v=mcg4qKbAmmY&t=316s&ab_channel=Fraz
3+
class Solution {
4+
public List<List<Integer>> subsetsWithDup(int[] nums) {
5+
Arrays.sort(nums);
6+
List<List<Integer>> ans = new ArrayList<>();
7+
ArrayList<Integer> list = new ArrayList<>();
8+
helper(ans, 0, nums, list, false);
9+
return ans;
10+
}
11+
12+
public void helper(List<List<Integer>> ans, int start, int[] nums, List<Integer> list, boolean ignored) {
13+
if (start>=nums.length) {
14+
ans.add(new ArrayList<>(list));
15+
} else {
16+
helper(ans, start+1, nums, list, true);
17+
//if we've ignored the value earlier then we must ignore all the values after that too
18+
if (start>0 && nums[start-1]==nums[start] && ignored)
19+
return;
20+
list.add(nums[start]);
21+
helper(ans, start+1, nums, list, false);
22+
list.remove(list.size()-1);
23+
}
24+
}
25+
26+
}

0 commit comments

Comments
 (0)