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.
Examples:
Input : abc Output : a, b, c, ab, bc, ac, abc Input : aaa Output : a, aa, aaa
Using Pick and Don’t Pick concept :
Python3
def printSubSequences(STR, subSTR=""): """ function: To print all subsequences of string concept: Pick and Don’t Pick variables: STR = string subSTR = to store subsequence """ if len(STR) == 0: print(subSTR, end=" ") return # we add adding 1st character in string printSubSequences(STR[:-1], subSTR + STR[-1]) """ Not adding first character of the string because the concept of subsequence either character will present or not """ printSubSequences(STR[:-1], subSTR) returndef main(): """ main function to drive code """ STR = "abc" printSubSequences(STR)if __name__ == "__main__": main()# by itsvinayak |
chevron_right
filter_none
Output:
cba cb ca c ba b a
pythonic implementations:
Prerequisite: itertools.combinations() module in Python to print all possible combinations
Python3
# Python Implementation of the approachimport itertoolsdef Sub_Sequences(STR): combs = [] for l in range(1, len(STR)+1): combs.append(list(itertools.combinations(STR, l))) for c in combs: for t in c: print (''.join(t), end =' ')# Testing with driver codeif __name__ == '__main__': Sub_Sequences('abc') |
chevron_right
filter_none
Output:
a b c ab ac bc abc
Recommended Posts:
- Print all subsequences of a string
- Print all subsequences of a string | Iterative Method
- Print all subsequences of a string using ArrayList
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Count subsequences in first string which are anagrams of the second string
- Construct a string that has exactly K subsequences from given string
- 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
- Generating all possible Subsequences using Recursion
- 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
- Find all subsequences with sum equals to K
- Sum of all subsequences of length K
- Maximum of minimum difference of all pairs from subsequences of given size
- Count of all subsequences having adjacent elements with different parity
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Number of subsequences in a string divisible by n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Improved By : itsvinayak

