diff --git a/src/main/java/com/thealgorithms/recursion/GenerateSubsets.java b/src/main/java/com/thealgorithms/recursion/GenerateSubsets.java
index 5a3ff2e88040..0114a55e5b75 100644
--- a/src/main/java/com/thealgorithms/recursion/GenerateSubsets.java
+++ b/src/main/java/com/thealgorithms/recursion/GenerateSubsets.java
@@ -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.
+ *
+ *
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 subsetRecursion(String str) {
- return doRecursion("", str);
+ return generateSubsets("", str);
}
- private static List doRecursion(String p, String up) {
- if (up.isEmpty()) {
- List 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 generateSubsets(String current, String remaining) {
+ if (remaining.isEmpty()) {
+ List result = new ArrayList<>();
+ result.add(current);
+ return result;
}
- // Taking the character
- char ch = up.charAt(0);
- // Adding the character in the recursion
- List left = doRecursion(p + ch, up.substring(1));
- // Not adding the character in the recursion
- List right = doRecursion(p, up.substring(1));
+ char ch = remaining.charAt(0);
+ String next = remaining.substring(1);
+
+ // Include the character
+ List withChar = generateSubsets(current + ch, next);
- left.addAll(right);
+ // Exclude the character
+ List withoutChar = generateSubsets(current, next);
- return left;
+ withChar.addAll(withoutChar);
+ return withChar;
}
}