All possible strings of any length that can be formed from a given string | Python3 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
from itertools import permutations NEW_LINE def printAll ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE opsize = pow ( 2 , n ) NEW_LINE for counter in range ( 1 , opsize ) : NEW_LINE INDENT subs = " " NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( counter & ( 1 << j ) ) : NEW_LINE INDENT subs += ( st [ j ] ) NEW_LINE DEDENT DEDENT perm = permutations ( subs ) NEW_LINE for i in perm : NEW_LINE INDENT print ( ' ' . join ( i ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " abc " NEW_LINE printAll ( ( st ) ) NEW_LINE DEDENT