Number of substrings having an equal number of lowercase and uppercase letters | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubstring ( string & S , int N ) { unordered_map < int , int > prevSum ; int res = 0 ; int currentSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] >= ' A ' and S [ i ] <= ' Z ' ) { currentSum ++ ; } else currentSum -- ; if ( currentSum == 0 ) res ++ ; if ( prevSum . find ( currentSum ) != prevSum . end ( ) ) { res += ( prevSum [ currentSum ] ) ; } prevSum [ currentSum ] ++ ; } return res ; } int main ( ) { string S = " gEEk " ; cout << countSubstring ( S , S . length ( ) ) ; return 0 ; }