-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerSet.java
More file actions
65 lines (49 loc) · 1.31 KB
/
PowerSet.java
File metadata and controls
65 lines (49 loc) · 1.31 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
/**
* Find the power set of a set
* @author Kavita
*
*/
public class PowerSet {
public static void main(String[] args) {
Set<String> powerSet=new HashSet<>();
powerSet.add("{}");
List<String> ls=new ArrayList<>();
ls.add("v");
ls.add("x");
ls.add("y");
ls.add("z");
for(int i=0; i<ls.size(); i++){
Iterator<String> iter=powerSet.iterator();
List<String> tmp=new ArrayList();
//add individual elements like "v", "x", "y"
tmp.add(ls.get(i));
//now append to existing sub-sets
while(iter.hasNext()){
String s=iter.next();
//we don't care about empty set
if(!s.equals("{}")){
//concatenate the set values
String tmpStr=s+""+ls.get(i);
//convert to character array to sort so that you don't have duplicates
char [] tmpChar=tmpStr.toCharArray();
Arrays.sort(tmpChar);
//add to tmp
tmp.add(String.valueOf(tmpChar));
}
}
//add all concatenations to powerset list
powerSet.addAll(tmp);
}
List<String> tmpList=new ArrayList<>(powerSet);
Collections.sort(tmpList);
System.out.println("Power set of "+ls+" is:");
System.out.println(tmpList);
}
}