- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathGenerateSubsets.java
36 lines (27 loc) · 940 Bytes
/
GenerateSubsets.java
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
packagecom.thealgorithms.recursion;
// program to find power set of a string
importjava.util.ArrayList;
importjava.util.List;
publicfinalclassGenerateSubsets {
privateGenerateSubsets() {
thrownewUnsupportedOperationException("Utility class");
}
publicstaticList<String> subsetRecursion(Stringstr) {
returndoRecursion("", str);
}
privatestaticList<String> doRecursion(Stringp, Stringup) {
if (up.isEmpty()) {
List<String> list = newArrayList<>();
list.add(p);
returnlist;
}
// Taking the character
charch = up.charAt(0);
// Adding the character in the recursion
List<String> left = doRecursion(p + ch, up.substring(1));
// Not adding the character in the recursion
List<String> right = doRecursion(p, up.substring(1));
left.addAll(right);
returnleft;
}
}