Question is to Print all the subsequences of a string in lexicographical order. So will the the given string be sorted or in the lexicographical order?
Understanding the question
Store your answer in an arraylist and then just sort the arraylist before printing.
Approach:
- Bigger Problem : To Print all of the possible subsequences.
-
Smaller Problem : Assume the recursion works and will give you ans for subsequences of indices 1 to n - 1 .
Self Work : In order to make you smaller problem your problem all you need to work for the 0th index element. Because it could be the part included in the answer or not.
Note : At the end sort the whole array.
yes i was going to use this method only , but since we will be using extra O(N) space i was wondering if there is an optimization to this
@shikhar07,
There isn’t. Because we need to preserve the order of characters in the string as well.
1 Like
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.