# Java program to find all the subsets of a string

The subsets of a string are all the possible combinations of its characters, including the empty string and the string itself. For example, the subsets of the string "abc" are "a", "b", "c", "ab", "ac", "bc", and "abc". The number of subsets of a string is equal to 2 to the power of the length of the string.

Given a string, we can treat it as a set of characters, and find all of its subsets. One way to do this is to use a binary representation of the subsets.

## Algorithm to find all the subsets of a string:

Step 1: Create an empty list result to store all the subsets.

Step 2: Get the length n of the input string str.

Step 3: Loop through all possible combinations of the characters using a binary representation of the numbers from 0 to 2^n-1.

Step 4: For each combination, create a new StringBuilder sb.

Step 5: Loop through all the characters in the string str using another loop variable j.

Step 6: Check if the j-th bit of the binary representation of the current combination is set (i.e., if (i & (1 << j)) > 0).

Step 7: If the bit is set, append the j-th character of the string to the StringBuilder.

Step 8: After the inner loop, add the resulting subset (i.e., the contents of the StringBuilder) to the result list.

Step 9: Return the result list after the outer loop has finished.

The algorithm outlines the basic steps needed to implement a Java program to find all the subsets of a string using a normal approach.

### Implementation:

Filename: SubsetsOfString.java

``````import java.util*;

public class SubsetsOfString {

public static void main(String[] args) {
String str = "abc";
List<String> subsets = getSubsets(str);
System.out.println("Subsets of \"" + str + "\": " + subsets);
}

public static List<String> getSubsets(String str) {
List<String> subsets = new ArrayList<>();
int n = str.length();
// Generate all possible binary strings of length n
for (int i = 0; i < (1 << n); i++) {
// Convert binary string to subset
StringBuilder subset = new StringBuilder();
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) > 0) {
subset.append(str.charAt(j));
}
}
// Add subset to list of subsets
}
return subsets;
}

}``````

Explanation:

The program uses a bit manipulation technique to generate all possible binary strings of length n. The expression `(1 << n)` computes 2^n, which is the number of possible binary strings. The loop variable `i` iterates over all possible binary strings, and the inner loop extracts the elements of the corresponding subset.

Output:

`Subsets of "abc": [, a, b, ab, c, ac, bc, abc]`

Complexity Analysis:

Time complexity: O(2^n), where n is the length of the string. Here there are 2^n possible subsets of a set with n elements, and each subset can be generated in constant time using bit manipulation.

Space complexity: O(2^n * n), where n is the length of the string. This is because there are 2^n possible subsets, and each subset can have up to n elements. The space required to store a single subset is proportional to its length, so the total space required is proportional to the product of 2^n and n.

## Approach: Using Recursion

FileName: SubsetsOfString.java

``````import java.util.*;

public class SubsetsOfString {

public static void main(String[] args) {
String str = "abc";
List<String> subsets = getSubsets(str);
System.out.println("Subsets of \"" + str + "\": " + subsets);
}

public static List<String> getSubsets(String str) {
List<String> subsets = new ArrayList<>();
// Call the recursive helper function to generate subsets
generateSubsets(str, 0, new StringBuilder(), subsets);
return subsets;
}

private static void generateSubsets(String str, int index, StringBuilder subset, List<String> subsets) {
// Base case: add the completed subset to the list of subsets
// Recursive case: generate subsets with the next element included or excluded
for (int i = index; i < str.length(); i++) {
// Include the next element
subset.append(str.charAt(i));
generateSubsets(str, i + 1, subset, subsets);
// Exclude the next element
subset.deleteCharAt(subset.length() - 1);
}
}

}
``````

Explanation:

The program uses a recursive approach to generate all possible subsets of a string. The `generateSubsets()` function is a recursive helper function that takes as input the original string, the current index of the string, the current subset being constructed, and a list of subsets. The function first adds the current subset to the list of subsets, and then calls itself twice: once with the next element of the string included in the subset, and once with the next element excluded.

Output:

`Subsets of "abc": [, c, b, bc, a, ac, ab, abc].`

Complexity Analysis:

Time complexity: O(2^n), where n is the length of the string. Here there are 2^n possible subsets of a set with n elements, and the recursive function generates all possible subsets. At each level of recursion, the function makes two recursive calls, so the total number of function calls is 2^n.

Space complexity: O(n * 2^n), where n is the length of the string. Here there are 2^n possible subsets, and each subset can have up to n elements. The space required to store a single subset is proportional to its length, so the total space required is proportional to the product of 2^n and n. The recursive function generates 2^n subsets, and each subset can have up to n elements, so the total space required is proportional to the product of n and 2^n.

## Approach: Iterative

FileName: SubsetsOfString.java

``````import java.util.ArrayList;
import java.util.List;

public class SubsetsOfString {
public static void main(String[] args) {
// Set the input string
String str = "abc";
System.out.println("Subsets of string " + str + " are:");

// Call the function to find the subsets of the string
List<String> subsets = findSubsets(str);

// Print the subsets
for (String subset : subsets) {
System.out.println(subset);
}
}

public static List<String> findSubsets(String str) {
// Create a list to store the subsets
List<String> subsets = new ArrayList<>();

// Add the empty string as the first subset

// Iterate over each character in the string
for (int i = 0; i < str.length(); i++) {
// Get the current size of the subset list
int size = subsets.size();

// Iterate over the existing subsets
for (int j = 0; j < size; j++) {
// Get the current subset
String subset = subsets.get(j);

// Add the current character to the subset to create a new subset
subset += str.charAt(i);

// Add the new subset to the list
}
}

// Return the list of subsets
return subsets;
}
}``````

Output:

``````a
b
ab
c
ac
bc
abc``````

Complexity Analysis:

• Time complexity: O(2^n), where n is the length of the input string. the algorithm generates 2^n possible subsets, one for each possible combination of including or excluding each character in the string.
• Space complexity: O(2^n), where n is the length of the input string. the algorithm needs to store all the subsets in memory, which requires space proportional to the number of subsets.