Count substrings of a given string whose anagram is a palindrome | Python3 program for the above approach ; Function to get the count of substrings whose anagrams are palindromic ; Store the answer ; Map to store the freq of masks ; Set frequency for mask 00. . .00 to 1 ; Store mask in x from 0 to i ; Update answer ; Update frequency ; Print the answer ; Driver Code ; Function call
from collections import defaultdict NEW_LINE def countSubstring ( s ) : NEW_LINE INDENT answer = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE m [ 0 ] = 1 NEW_LINE x = 0 NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT x ^= 1 << ( ord ( s [ j ] ) - ord ( ' a ' ) ) NEW_LINE answer += m [ x ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT answer += m [ x ^ ( 1 << i ) ] NEW_LINE DEDENT m [ x ] += 1 NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT str = " abab " NEW_LINE countSubstring ( str ) NEW_LINE