-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount the Substrings.cpp
63 lines (56 loc) · 1.6 KB
/
Count the Substrings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//{ Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
/*
For input 1 WomensDAY
1. Replace each uppercase character with 1 and lower case character with -1.
0 1 -1 -1 -1 -1 -1 1 1 1
Any subarray having sum 0 will be having equal number of upper case and lower case characters.
After the map declaration, we store zero with count 1;
2. Use map and prefix sum to count the number of subarrays having zero sum.
0 1 0 -1 -2 -3 -4 -3 -2 -1
3. In above prefix sum, whenever a sum repeats itself, it means that there is a zero sum subarray.
*/
class Solution
{
public:
int countSubstring(string S)
{
unordered_map<int,int> map;
map[0]=1;
int ans=0;
int sum=0;
for(char ch:S){
int val;
if(isupper(ch)){
val=1;
}
else{
val=-1;
}
sum+=val;
if(map.find(sum)!=map.end()){ // If map.find(sum) != map.end(), it means that the key sum is found in the map, and there is at least one element with that key. If map.find(sum) == map.end(), it means that the key sum is not found in the map, and there are no elements with that key.
ans+=map[sum];
}
map[sum]++;
}
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
string S;
cin>>S;
Solution obj;
cout<<obj.countSubstring(S)<<endl;
}
}
// } Driver Code Ends