text: Count of distinct permutation of a String obtained by swapping only unequal characters | Importing Dictionary class ; 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 )
code: using System ; using System . Collections . Generic ; public class GFG { static int validPermutations ( String str ) { Dictionary < char , int > m = new Dictionary < char , int > ( ) ; int count = str . Length , ans = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( m . ContainsKey ( str [ i ] ) ) m [ str [ i ] ] = m [ str [ i ] ] + 1 ; else m . Add ( str [ i ] , 1 ) ; } for ( int i = 0 ; i < str . Length ; i ++ ) { ans += count - m [ str [ i ] ] ; if ( m . ContainsKey ( str [ i ] ) ) m [ str [ i ] ] = m [ str [ i ] ] - 1 ; count -- ; } return ans + 1 ; } public static void Main ( String [ ] args ) { String str = " sstt " ; Console . WriteLine ( validPermutations ( str ) ) ; } }