Given a string, we have to find out all subsequences of it. A String is a subsequence of a given String, that is generated by deleting some character of a given string without changing its order.
Input : abc Output : a, b, c, ab, bc, ac, abc Input : aaa Output : a, aa, aaa
Step 1: Iterate over the entire String Step 2: Iterate from the end of string in order to generate different substring add the subtring to the list Step 3: Drop kth character from the substring obtained from above to generate different subsequence. Step 4: if the subsequence is not in the list then recur.
Below is the implementation of the approach.
[aa, a, ab, bc, ac, b, aac, abc, c, aab, aabc]
Alternate Solution :
One by one fix characters and recursively generates all subsets starting from them. After every recursive call, we remove last character so that next permutation can be generated.
c ca cab cb a ab b
This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Print all subsequences of a string using ArrayList
- Print all subsequences of a string | Iterative Method
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Count subsequences in first string which are anagrams of the second string
- Count of 'GFG' Subsequences in the given string
- Number of subsequences in a string divisible by n
- Lexicographically smallest permutation of a string with given subsequences
- Number of subsequences as "ab" in a string repeated K times
- Number of ways to partition a string into two balanced subsequences
- Generating distinct subsequences of a given string in lexicographic order
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Print shortest path to print a string on screen
- Print the longest prefix of the given string which is also the suffix of the same string
Improved By : princiraj1992