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
function shouldSwap ( str , start , curr ) { for ( let i = start ; i < curr ; i ++ ) { if ( str [ i ] == str [ curr ] ) { return false ; } } return true ; } function findPermutations ( str , index , n ) { if ( index >= n ) { document . write ( str ) ; document . write ( " " ) return ; } for ( let i = index ; i < n ; i ++ ) { let check = shouldSwap ( str , index , i ) ; if ( check ) { swap ( str , index , i ) ; findPermutations ( str , index + 1 , n ) ; swap ( str , index , i ) ; } } } function swap ( str , i , j ) { let c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } let str = [ ' ' , ' ' , ' ' , ' ' ] ; let n = str . length ; findPermutations ( str , 0 , n ) ;