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
function countSubstring ( S , N ) { var prevSum = new Map ( ) ; var res = 0 ; var currentSum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( S [ i ] >= ' ' && S [ i ] <= ' ' ) { currentSum ++ ; } else currentSum -- ; if ( currentSum == 0 ) res ++ ; if ( prevSum . has ( currentSum ) ) { res += ( prevSum . get ( currentSum ) ) ; } if ( prevSum . has ( currentSum ) ) prevSum . set ( currentSum , prevSum . get ( currentSum ) + 1 ) else prevSum . set ( currentSum , 1 ) } return res ; } var S = " " ; document . write ( countSubstring ( S , S . length ) ) ;