Number of substrings having an equal number of lowercase and uppercase letters | Function to find the count of substrings having an equal number of uppercase and lowercase characters ; Stores the count of prefixes having sum S considering uppercase and lowercase characters as 1 and - 1 ; Stores the count of substrings having equal number of lowercase and uppercase characters ; Stores the sum obtained so far ; If the character is uppercase ; Otherwise ; If currsum is o ; If the current sum exists in the HashMap prevSum ; Increment the resultant count by 1 ; Update the frequency of the current sum by 1 ; Return the resultant count of the subarrays ; Driver Code
def countSubstring ( S , N ) : NEW_LINE INDENT prevSum = { } NEW_LINE res = 0 NEW_LINE currentSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] >= ' A ' and S [ i ] <= ' Z ' ) : NEW_LINE INDENT currentSum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT currentSum -= 1 NEW_LINE DEDENT if ( currentSum == 0 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( currentSum in prevSum ) : NEW_LINE INDENT res += ( prevSum [ currentSum ] ) NEW_LINE DEDENT if currentSum in prevSum : NEW_LINE INDENT prevSum [ currentSum ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prevSum [ currentSum ] = 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " gEEk " NEW_LINE print ( countSubstring ( S , len ( S ) ) ) NEW_LINE DEDENT