Given a string of distinct characters, print all possible strings of any length that can be formed from given string characters.

Examples:

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++.

`/* C++ code to generate all possible strings ` ` ` `that can be formed from given string */`
`#include<bits/stdc++.h> ` `using` `namespace` `std; `
` ` `void` `printAll(string str) `
`{ ` ` ` `/* Number of subsequences is (2**n -1)*/`
` ` `int` `n = str.length(); `
` ` `unsigned ` `int` `opsize = ` `pow` `(2, n); `
` ` ` ` `/* Generate all subsequences of a given string. `
` ` `using counter 000..1 to 111..1*/`
` ` `for` `(` `int` `counter = 1; counter < opsize; counter++) `
` ` `{ `
` ` `string subs = ` `""` `; `
` ` `for` `(` `int` `j = 0; j < n; j++) `
` ` `{ `
` ` `/* Check if jth bit in the counter is set `
` ` `If set then print jth element from arr[] */`
` ` `if` `(counter & (1<<j)) `
` ` `subs.push_back(str[j]); `
` ` `} `
` ` ` ` `/* Print all permutations of current subsequence */`
` ` `do`
` ` `{ `
` ` `cout << subs << ` `" "` `; `
` ` `} `
` ` `while` `(next_permutation(subs.begin(), subs.end())); `
` ` `} `
`} ` ` ` `// Driver program ` `int` `main() `
`{ ` ` ` `string str = ` `"abc"` `; `
` ` `printSubsequences(str); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python3 code to generate all possible strings ` `# that can be formed from given string ` `from` `itertools ` `import` `permutations `
` ` `def` `printAll( st): `
` ` ` ` `# Number of subsequences is (2**n -1) `
` ` `n ` `=` `len` `(st) `
` ` `opsize ` `=` `pow` `(` `2` `, n) `
` ` ` ` `# Generate all subsequences of a given string. `
` ` `# using counter 000..1 to 111..1 `
` ` `for` `counter ` `in` `range` `(` `1` `, opsize): `
` ` ` ` `subs ` `=` `"" `
` ` `for` `j ` `in` `range` `(n): `
` ` ` ` `# Check if jth bit in the counter is set `
` ` `# If set then print jth element from arr[] `
` ` `if` `(counter & (` `1` `<<j)): `
` ` `subs ` `+` `=` `(st[j]) `
` ` ` ` `# Print all permutations of current subsequence `
` ` `perm ` `=` `permutations(subs) `
` ` ` ` `for` `i ` `in` `perm: `
` ` `print` `(''.join(i),end` `=` `" "` `) `
` ` `# Driver program ` `if` `__name__ ` `=` `=` `"__main__"` `: `
` ` ` ` `st ` `=` `"abc"`
` ` `printAll((st)) `
` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

Output:

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 contribute@geeksforgeeks.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.

## Recommended Posts:

- Print all possible palindromic string formed using any pair of given strings
- Print all possible strings of length k that can be formed from a set of n characters
- Longest palindromic String formed using concatenation of given strings in any order
- Maximize count of strings of length 3 that can be formed from N 1s and M 0s
- Check if given string can be formed by two other strings or their permutations
- Check if K palindromic strings can be formed from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Strings formed from given characters without any consecutive repeating characters
- Count of strings that can be formed from another string using each character at-most once
- Check if characters of a given string can be used to form any N equal strings
- String matching with * (that matches with any) in any of the two strings
- Count of strings that can be formed using a, b and c under given constraints
- Maximum number of strings that can be formed with given zeros and ones
- Largest even number that can be formed by any number of swaps
- K length words that can be formed from given characters without repetition
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Count of sub-strings of length n possible from the given string
- Minimum length of Run Length Encoding possible by removing at most K characters from a given string