Subsequence : If abc is a string then subsequence are - a,ab,abc,ac,b,bc,c,"".So lets find out first how we can divide this problem in suproblem .think if we split first character then the string becomes "bc".then if we find out the sequence of bc it becomes-b,bc,c,"".Now lets concatenate first splitted character "a" with the result of sequence bc ,then we will get our desired output .
public class App {
public static void main(String[] args) {
String word = "bc";
String subSequence = getSubSequence(word);
System.out.println(subSequence);
}
private static String getSubSequence(String word) {
if (word.isEmpty()) {
return "";
}
char first = word.charAt(0);
System.out.println(first);
String rest = getSubSequence(word.substring(1));
String result = "";
for (String subSeq : rest.split(",", -1)) {
result += "," + first + subSeq;
result += "," + subSeq;
}
return result.substring(1);
}
}
No comments:
Post a Comment