Given a string of distinct characters, print all possible strings of any length that can be formed from given string characters.
Input: abc Output: a b c abc ab ac bc bac bca cb ca ba cab cba acb Input: abcd Output: a b ab ba c ac ca bc cb abc acb bac bca cab cba d ad da bd db abd adb bad bda dab dba cd dc acd adc cad cda dac dca bcd bdc cbd cdb dbc dcb abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdca cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba
The generation of all strings include the following steps.
1) Generate all subsequences of given string.
2) For every subsequence ‘subs’, print all permutations of ‘subs’
Below is C++ implementation. It uses next_permutation function in C++.
a b ab ba c ac ca bc cb abc acb bac bca cab cba
This article is contributed by Hardik Gaur. 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 possible strings of length k that can be formed from a set of n characters
- Count of strings that can be formed from another string using each character at-most once
- Check if given string can be formed by two other strings or their permutations
- Find the number of strings formed using distinct characters of a given string
- Count of sub-strings of length n possible from the given string
- Number of strings of length N with no palindromic sub string
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- LCS formed by consecutive segments of at least length K
- K length words that can be formed from given characters without repetition
- Count of strings that can be formed using a, b and c under given constraints
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Find the numbers of strings that can be formed after processing Q queries
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find all strings formed from characters mapped to digits of a number
Improved By : Akanksha_Rai