Skip to content

refactor: cleanup GenerateSubsets #6373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/main/java/com/thealgorithms/recursion/GenerateSubsets.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
package com.thealgorithms.recursion;

// program to find power set of a string

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() {
throw new UnsupportedOperationException("Utility class");
}

/**
* 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 doRecursion("", str);
return generateSubsets("", str);
}

private static List<String> doRecursion(String p, String up) {
if (up.isEmpty()) {
List<String> list = new ArrayList<>();
list.add(p);
return list;
/**
* 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;
}

// 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));
char ch = remaining.charAt(0);
String next = remaining.substring(1);

// Include the character
List<String> withChar = generateSubsets(current + ch, next);

left.addAll(right);
// Exclude the character
List<String> withoutChar = generateSubsets(current, next);

return left;
withChar.addAll(withoutChar);
return withChar;
}
}
Loading
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