text: Count of distinct permutation of a String obtained by swapping only unequal characters | Function to calculate total number of valid permutations ; Creating count which is equal to the Total number of characters present and ans that will store the number of unique permutations ; Storing frequency of each character present in the string ; Adding count of characters by excluding characters equal to current char ; Reduce the frequency of the current character and count by 1 , so that it cannot interfere with the calculations of the same elements present to the right of it . ; Return ans + 1 ( Because the given string is also a unique permutation ) ; Driver Code
code: function validPermutations ( str ) { let m = new Map ( ) ; let count = str . length , ans = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( m . has ( str [ i ] ) ) { m . set ( str [ i ] , m . get ( str [ i ] ) + 1 ) ; } else { m . set ( str [ i ] , 1 ) ; } } for ( let i = 0 ; i < str . length ; i ++ ) { ans += count - m . get ( str [ i ] ) ; m . set ( str [ i ] , m . get ( str [ i ] ) - 1 ) ; count -- ; } return ans + 1 ; } let str = " " ; document . write ( validPermutations ( str ) ) ;