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