forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenerateSubsets.java
More file actions
52 lines (43 loc) · 1.52 KB
/
GenerateSubsets.java
File metadata and controls
52 lines (43 loc) · 1.52 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
package com.thealgorithms.recursion;
import java.util.ArrayList;
import java.util.List;
/**
* Utility class to generate all subsets (power set) of a given string using recursion.
*
* <p>For example, the string "ab" will produce: ["ab", "a", "b", ""]
*/
public final class GenerateSubsets {
private GenerateSubsets() {
}
/**
* Generates all subsets (power set) of the given string using recursion.
*
* @param str the input string to generate subsets for
* @return a list of all subsets of the input string
*/
public static List<String> subsetRecursion(String str) {
return generateSubsets("", str);
}
/**
* Recursive helper method to generate subsets by including or excluding characters.
*
* @param current the current prefix being built
* @param remaining the remaining string to process
* @return list of subsets formed from current and remaining
*/
private static List<String> generateSubsets(String current, String remaining) {
if (remaining.isEmpty()) {
List<String> result = new ArrayList<>();
result.add(current);
return result;
}
char ch = remaining.charAt(0);
String next = remaining.substring(1);
// Include the character
List<String> withChar = generateSubsets(current + ch, next);
// Exclude the character
List<String> withoutChar = generateSubsets(current, next);
withChar.addAll(withoutChar);
return withChar;
}
}