-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathGenerateSubsets.java
41 lines (31 loc) · 1.05 KB
/
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
37
38
39
40
41
package com.thealgorithms.Recursion;
// program to find power set of a string
import java.util.ArrayList;
import java.util.List;
/**
* Finds all permutations of given array
* @author Tuhin Mondal (<a href="https://github.com/tuhinm2002">Git-Tuhin Mondal</a>)
*/
public final class GenerateSubsets {
private GenerateSubsets() {
throw new UnsupportedOperationException("Utility class");
}
public static List<String> subsetRecursion(String str) {
return doRecursion("", str);
}
private static List<String> doRecursion(String p, String up) {
if (up.isEmpty()) {
List<String> list = new ArrayList<>();
list.add(p);
return list;
}
// Taking the character
char ch = 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);
return left;
}
}