Skip to content
Open
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
25 changes: 25 additions & 0 deletions Misc/String_subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class AllSubsets {
public static void main(String[] args) {

String str = "FUN";
int len = str.length();
int temp = 0;
//Total possible subsets for string of size n is n*(n+1)/2
String arr[] = new String[len*(len+1)/2];

//This loop maintains the starting character
for(int i = 0; i < len; i++) {
//This loop adds the next character every iteration for the subset to form and add it to the array
for(int j = i; j < len; j++) {
arr[temp] = str.substring(i, j+1);
temp++;
}
}

//This loop prints all the subsets formed from the string.
System.out.println("All subsets for given string are: ");
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}