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 the 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Print all subsequences of a string | Iterative Method
- Print all subsequences of a string using ArrayList
- Print all subsequences of a string in Python
- Print all possible K-length subsequences of first N natural numbers with sum N
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Minimize deletions in a Binary String to remove all subsequences of the form "0101"
- Count subsequences in first string which are anagrams of the second string
- Construct a string that has exactly K subsequences from given string
- Check if a string can be obtained by appending subsequences of another string
- Check if string S2 can be obtained by appending subsequences of string S1
- Count all increasing subsequences
- Sum of all subsequences of an array
- Count all subsequences having product less than K
- Sum of all subsequences of a number
- Sum of width (max and min diff) of all Subsequences
- Product of all Subsequences of size K except the minimum and maximum Elements
- Find all combinations of two equal sum subsequences
- Sum of all subsequences of length K
- Maximum of minimum difference of all pairs from subsequences of given size