Skip to content

Commit 182118b

Browse files
refactor: cleanup GenerateSubsets (#6373)
refactor: cleanup GenerateSubsets Co-authored-by: Deniz Altunkapan <93663085+DenizAltunkapan@users.noreply.github.com>
1 parent 910d5b8 commit 182118b

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed
Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
package com.thealgorithms.recursion;
22

3-
// program to find power set of a string
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

6+
/**
7+
* Utility class to generate all subsets (power set) of a given string using recursion.
8+
*
9+
* <p>For example, the string "ab" will produce: ["ab", "a", "b", ""]
10+
*/
811
public final class GenerateSubsets {
912

1013
private GenerateSubsets() {
11-
throw new UnsupportedOperationException("Utility class");
1214
}
1315

16+
/**
17+
* Generates all subsets (power set) of the given string using recursion.
18+
*
19+
* @param str the input string to generate subsets for
20+
* @return a list of all subsets of the input string
21+
*/
1422
public static List<String> subsetRecursion(String str) {
15-
return doRecursion("", str);
23+
return generateSubsets("", str);
1624
}
1725

18-
private static List<String> doRecursion(String p, String up) {
19-
if (up.isEmpty()) {
20-
List<String> list = new ArrayList<>();
21-
list.add(p);
22-
return list;
26+
/**
27+
* Recursive helper method to generate subsets by including or excluding characters.
28+
*
29+
* @param current the current prefix being built
30+
* @param remaining the remaining string to process
31+
* @return list of subsets formed from current and remaining
32+
*/
33+
private static List<String> generateSubsets(String current, String remaining) {
34+
if (remaining.isEmpty()) {
35+
List<String> result = new ArrayList<>();
36+
result.add(current);
37+
return result;
2338
}
2439

25-
// Taking the character
26-
char ch = up.charAt(0);
27-
// Adding the character in the recursion
28-
List<String> left = doRecursion(p + ch, up.substring(1));
29-
// Not adding the character in the recursion
30-
List<String> right = doRecursion(p, up.substring(1));
40+
char ch = remaining.charAt(0);
41+
String next = remaining.substring(1);
42+
43+
// Include the character
44+
List<String> withChar = generateSubsets(current + ch, next);
3145

32-
left.addAll(right);
46+
// Exclude the character
47+
List<String> withoutChar = generateSubsets(current, next);
3348

34-
return left;
49+
withChar.addAll(withoutChar);
50+
return withChar;
3551
}
3652
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy