-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSubSets2.java
More file actions
39 lines (30 loc) · 963 Bytes
/
SubSets2.java
File metadata and controls
39 lines (30 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package Backtracking;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 27/10/18
* Time - 1:02 PM
*/
public class SubSets2 {
public void addSubsets(ArrayList<Integer> A, int pos, Map<ArrayList<Integer>,Boolean> map,
ArrayList<Integer> temp){
if(!map.containsKey(temp)){
map.put(new ArrayList<>(temp), true);
}
for(int i=pos;i<A.size();i++){
temp.add(A.get(i));
addSubsets(A,i+1,map,temp);
temp.remove(temp.size()-1);
}
}
public ArrayList<ArrayList<Integer>> subsetsWithDup(ArrayList<Integer> A) {
Map<ArrayList<Integer>,Boolean> map = new LinkedHashMap<>();
ArrayList<Integer> temp = new ArrayList<>();
Collections.sort(A);
addSubsets(A,0,map,temp);
return new ArrayList<>(map.keySet());
}
}