human
Number of index pairs such that s [ i ] and s [ j ] are anagrams | Function to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] ..
To store the count of sorted strings.
Traverse all strings and store in the map.
Sort the string.
If string exists in map , increment count Else create key value pair with count = 1.
To store the number of pairs.
Traverse through the map.
Count the pairs for each string.
Return the required answer.
Driver code.
Function call"
How can the above be solved in Python?
assistant
#Here is code to number of index pairs such that s [ i ] and s [ j ] are anagrams and the other steps required.
 def anagram_pairs ( s , n ) :
	mp = dict ( )
	for i in range ( n ) :
		temp_str = " " . join ( sorted ( s [ i ] ) )
		if temp_str in mp :
			mp [ temp_str ] += 1
		else :
			mp [ temp_str ] = 1
	ans = 0
	for k in mp . values ( ) :
		ans += ( k * ( k - 1 ) ) // 2
	return ans
if __name__ == " _ _ main _ _ " :
	s = [ " aaab " , " aaba " , " baaa " , " cde " , " dec " ]
	n = len ( s )
	print ( anagram_pairs ( s , n ) )