I need the code (or snippet of the code for the following problem or hint on how to solve):
A sub sequence of string s is obtained by deleting one or more characters from s. The set of sub sequences for string s = abc
would be a, ab, ac, abc, b, bc, c,
and the empty string (which is a sub sequence of all strings).
Find all possible sub sequences for s and print them in lexicographic order.
This is what i have come out with but it's not working:
TreeSet<String> ls = new TreeSet<>();
for(int i=0;i<s.length();i++)
{
for(int j=i;j<s.length();j++)
{
StringBuffer sb = new StringBuffer();
for(int k=i;k<j+1;k++)
{
sb.append(s.charAt(k));
}
ls.add(sb.toString());
}
}
return ls.toArray(new String[ls.size()]);
Result:
TestCase : abc
Output: a ab abc b bc c
Expected Output: a ab abc ac b bc c