# All possible strings of any length that can be formed from a given string

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
bdac bdca cabd cadb cbad cbda cdab cdba
dabc dacb dbac dbca dcab dcba
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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/C++

 `/*  C++ code to generate all possible strings ` `    ``that can be formed from given string */` `#include ` `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<

## Python3

 `# 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``<

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.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Improved By : Akanksha_Rai, chitranayal

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.