Given string S consisting of lowercase and uppercase letters, the task is to find the number of substrings having the equal number of lowercase and uppercase letters.
Examples:
Input: S = “gEEk”
Output: 3
Explanation:
Following are the substrings having the equal number of lowercase and uppercase letters:
- “gE”
- “gEEk”
- “Ek”
Therefore, the total count of substrings is 3.
Input: S = “WomensDAY”
Output: 4
Naive Approach: The simplest approach to solve the given problem is to generate all possible substring of the given string S and increment the count of substring by 1 if the that substring contains equal number of lowercase and uppercase letters. After checking for all the substrings, print the value of count as the result.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The given problem can be solved by considering each lowercase and uppercase letters as 1 and -1 respectively and then find the count of subarray having sum 0. Follow the steps to solve the problem:
- Initialize a HashMap, say M that stores the frequency of sum of all the prefixes.
- Initialize a variable say, currentSum as 0 and res as 0 that stores the sum of each prefix and count of resultant substring respectively.
- Traverse the string and perform the following steps:
- If the current character is uppercase, then increment the value of currentSum by 1. Otherwise, decrement the value of currentSum by -1.
- If the value of currentSum is 0, then increment the value of res by 1.
- If the value of currentSum exists in the map M, then increment the value of res by M[currentSum].
- Increment the frequency of currentSum in the HashMap M by 1.
- After completing the above steps, print the value of res as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the count of// substrings having an equal number// of uppercase and lowercase charactersint countSubstring(string& S, int N){ // Stores the count of prefixes // having sum S considering uppercase // and lowercase characters as 1 and -1 unordered_map<int, int> prevSum; // Stores the count of substrings // having equal number of lowercase // and uppercase characters int res = 0; // Stores the sum obtained so far int currentSum = 0; for (int i = 0; i < N; i++) { // If the character is uppercase if (S[i] >= 'A' and S[i] <= 'Z') { currentSum++; } // Otherwise else currentSum--; // If currsum is o if (currentSum == 0) res++; // If the current sum exists in // the HashMap prevSum if (prevSum.find(currentSum) != prevSum.end()) { // Increment the resultant // count by 1 res += (prevSum[currentSum]); } // Update the frequency of the // current sum by 1 prevSum[currentSum]++; } // Return the resultant count of // the subarrays return res;} // Driver Codeint main(){ string S = "gEEk"; cout << countSubstring(S, S.length()); return 0;} |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


