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
Method 1 (Pick and Don’t Pick Concept)
[abcd, abc, abd, ab, acd, ac, ad, a, bcd, bc, bd, b, cd, c, d, ]
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]
Method 3 :
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 email@example.com. 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 in Python
- 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
- Number of subsequences as "ab" in a string repeated K times
- Number of subsequences in a given binary string divisible by 2
- Lexicographically smallest permutation of a string with given subsequences
- Number of ways to partition a string into two balanced subsequences
- Minimum number of subsequences required to convert one string to another
- Generating distinct subsequences of a given string in lexicographic order
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum number of palindromic subsequences to be removed to empty a binary string