Home >>Java Programs >Java Program to find all subsets of a string
In this example, we will create a java program to find all the subsets of a given string and print them.
The subset of a string is the character or the group of characters that are present inside the given string. Any string can have n(n+1)/2 possible subsets.
public class Main
{
public static void main(String[] args)
{
String str = "Abhimanyu";
int len = str.length();
int temp = 0;
String arr[] = new String[len*(len+1)/2];
for(int i = 0; i < len; i++)
{
for(int j = i; j < len; j++)
{
arr[temp] = str.substring(i, j+1);
temp++;
}
}
System.out.println("All subsets for given string are: ");
for(int i = 0; i < arr.length; i++)
{
System.out.println(arr[i]);
}
}
}