Number of substrings with each character occurring even times | Function to count substrings having even frequency of each character ; Stores the total count of substrings ; Traverse the range [ 0 , N ] : ; Traverse the range [ i + 1 , N ] ; Stores the substring over the range of indices [ i , len ] ; Stores the frequency of characters ; Count frequency of each character ; Traverse the dictionary ; If any of the keys have odd count ; Otherwise ; Return count ; Driver Code
def subString ( s , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for len in range ( i + 1 , n + 1 ) : NEW_LINE INDENT test_str = ( s [ i : len ] ) NEW_LINE res = { } NEW_LINE for keys in test_str : NEW_LINE INDENT res [ keys ] = res . get ( keys , 0 ) + 1 NEW_LINE DEDENT flag = 0 NEW_LINE for keys in res : NEW_LINE INDENT if res [ keys ] % 2 != 0 : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if flag == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT S = " abbaa " NEW_LINE N = len ( S ) NEW_LINE print ( subString ( S , N ) ) NEW_LINE