Proceed further for str [ i ] only if it doesn 't match with any of the characters  after str[index]
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 ; }