-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFG_POTD2.cpp
More file actions
37 lines (35 loc) · 935 Bytes
/
GFG_POTD2.cpp
File metadata and controls
37 lines (35 loc) · 935 Bytes
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
Question-- >> Count number of substrings
https : // practice.geeksforgeeks.org/problems/count-number-of-substrings4528/1
// User function template for C++
class Solution
{
public:
long long int upto(string s, int k)
{
int b = 0, p = 0, count = 0;
long long ans = 0;
vector<int> ab(26, 0);
while (p < s.length())
{
ab[s[p] - 'a']++;
if (ab[s[p] - 'a'] == 1)
count++;
while (count > k)
{
ab[s[b] - 'a']--;
if (ab[s[b] - 'a'] <= 0)
{
count--;
}
b++;
}
ans += p - b + 1;
p++;
}
return ans;
}
long long int substrCount(string s, int k)
{
return upto(s, k) - upto(s, k - 1);
}
};