Generate all permutations of a string that follow given constraints | Simple Python program to print all permutations of a string that follow given constraint ; Check if current permutation is valid ; Recursively generate all permutation ; Driver Code
def permute ( str , l , r ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT if " AB " not in ' ' . join ( str ) : NEW_LINE INDENT print ( ' ' . join ( str ) , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT for i in range ( l , r + 1 ) : NEW_LINE INDENT str [ l ] , str [ i ] = str [ i ] , str [ l ] NEW_LINE permute ( str , l + 1 , r ) NEW_LINE str [ l ] , str [ i ] = str [ i ] , str [ l ] NEW_LINE DEDENT DEDENT str = " ABC " NEW_LINE permute ( list ( str ) , 0 , len ( str ) - 1 ) NEW_LINE