Distinct permutations of the string | Set 2 | Returns true if str [ curr ] does not matches with any of the characters after str [ start ] ; Prints all distinct permutations in str [ 0. . n - 1 ] ; Proceed further for str [ i ] only if it doesn 't match with any of  the characters after str[index] ; Driver code
def shouldSwap ( string , start , curr ) : NEW_LINE INDENT for i in range ( start , curr ) : NEW_LINE INDENT if string [ i ] == string [ curr ] : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def findPermutations ( string , index , n ) : NEW_LINE INDENT if index >= n : NEW_LINE INDENT print ( ' ' . join ( string ) ) NEW_LINE return NEW_LINE DEDENT for i in range ( index , n ) : NEW_LINE INDENT check = shouldSwap ( string , index , i ) NEW_LINE if check : NEW_LINE INDENT string [ index ] , string [ i ] = string [ i ] , string [ index ] NEW_LINE findPermutations ( string , index + 1 , n ) NEW_LINE string [ index ] , string [ i ] = string [ i ] , string [ index ] NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = list ( " ABCA " ) NEW_LINE n = len ( string ) NEW_LINE findPermutations ( string , 0 , n ) NEW_LINE DEDENT