Java Program to find all subsets of a string
All the possible subsets for a string will be n(n+1)/2.
Example:
Input: str=JAVA
Output: All subsets for given string are:
J, JA, JAV, JAVA, A, AV, AVA, V, VA, A
Approach
Java
public class StringAllSubset {public static void main(String[] args) {String str = "JAVA";int len = str.length();int temp = 0;// Total possible subsets formula= n*(n+1)/2String arr[] = new String[len * (len + 1) / 2];// This loop maintains the starting characterfor (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]);}}}
No comments:
Post a Comment