From f927ef3712ee1a1d62666fe8249ca0d3f3c0b1c3 Mon Sep 17 00:00:00 2001 From: GyanPrakashSinghIIITG1997 <56785387+GyanPrakashSinghIIITG1997@users.noreply.github.com> Date: Mon, 21 Oct 2019 15:52:00 +0530 Subject: [PATCH] Create String_subsets.java --- Misc/String_subsets.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Misc/String_subsets.java diff --git a/Misc/String_subsets.java b/Misc/String_subsets.java new file mode 100644 index 000000000000..6510be4467f2 --- /dev/null +++ b/Misc/String_subsets.java @@ -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]); + } + } +}