public static void subseq(String str, String result) {
if(str.length()==0) {
return ;
}
char cc = str.charAt(0);
String ros = str.substring(1);
subseq(ros,result);
subseq(ros,result+cc);
System.out.println(result);
}
public static void subseq(String str, String result) {
if(str.length()==0) {
return ;
}
char cc = str.charAt(0);
String ros = str.substring(1);
subseq(ros,result);
subseq(ros,result+cc);
System.out.println(result);
}
Hi Vikash,
you need to put you print statement inside the if(str.length==0) block, not at last. After that you will get the correct answer but the strings won’t be in lexicographical order. So for that, store your answers in a arraylist and then sort the arraylist.
sir i am not able to think of a way to add a arraylist in it , and store the elements in it , please help me
public static void subseq(String str, String result) {
if (str.length() == 0) {
System.out.println(result);
return;
}
char cc = str.charAt(0);
String ros = str.substring(1);
subseq(ros, result);
subseq(ros, result + cc);
}
If you this approach you will get all the subsequences but not in lexicographical order. And for sorting the arraylist check how I have implemented it: https://ide.codingblocks.com/s/140500