Number of index pairs such that s [ i ] and s [ j ] are anagrams | C # program to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; Function to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; To store count of strings ; Traverse all strings and store in the map ; Sort the string ; Store in the map ; To store the number of pairs ; Traverse through the map ; Count the pairs for each string ; Return the required answer ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int anagram_pairs ( String [ ] s , int n ) { Dictionary < String , int > mp = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char [ ] chArr = s [ i ] . ToCharArray ( ) ; Array . Sort ( chArr ) ; s [ i ] = new String ( chArr ) ; if ( mp . ContainsKey ( s [ i ] ) ) { mp [ s [ i ] ] = mp [ s [ i ] ] + 1 ; } else { mp . Add ( s [ i ] , 1 ) ; } } int ans = 0 ; foreach ( KeyValuePair < String , int > i in mp ) { int k = i . Value ; ans += ( k * ( k - 1 ) ) / 2 ; } return ans ; } public static void Main ( String [ ] args ) { String [ ] s = { " aaab " , " aaba " , " baaa " , " cde " , " dec " } ; int n = s . Length ; Console . WriteLine ( anagram_pairs ( s , n ) ) ; } }