Given a string str, the task is to print all the sub-sequences of str.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Input: str = “abc”
Output: a b ab c ac bc abc
Input: str = “geek”
Output: g e ge e ge ee gee k gk ek gek ek gek eek geek
Approach: Write a recursive function that prints every sub-sequence of the sub-string starting from the second character str[1, n – 1] after appending the first character of the string str in the beginning of every sub-sequence. Terminating condition will be when the passed string is empty, in that case the function will return an empty arraylist.
Below is the implementation of the above approach:
g e ge e ge ee gee k gk ek gek ek gek eek geek
Alternate Solution: One by one fix characters and recursively generate all subsets starting from them.
c b bc a ac ab abc
- ArrayList of ArrayList in Java
- Print all permutation of a string using ArrayList
- Print all subsequences of a string | Iterative Method
- Print all subsequences of a string in Python
- Print all subsequences of a string
- Java ArrayList to print all possible words from phone digits
- Convert an ArrayList of String to a String array in Java
- 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"
- Remove all elements from the ArrayList in Java
- How to add all items from a collection to an ArrayList in Java?
- Count subsequences in first string which are anagrams of the second string
- Construct a string that has exactly K subsequences from given string
- Generate all distinct subsequences of array using backtracking
- Generating all possible Subsequences using Recursion
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Graph Representation using Java ArrayList
- Count all increasing subsequences
- Sum of all subsequences of an array
- Count all subsequences having product less than K
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.