-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count number of Substrings which has exactly k distinct characters
72 lines (49 loc) · 1.88 KB
/
Count number of Substrings which has exactly k distinct characters
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
64
65
66
67
68
69
70
71
72
https://practice.geeksforgeeks.org/contest/interview-series-45/problems/#
Edutorial :
1) Hashmap + Sliding Window -->> https://iq.opengenus.org/substrings-with-k-distinct-characters/
2) Freq[26] + Sliding Window -->> youtube.com/watch?v=rTefnFlCwpI
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
long long int helper(string s, int k){
// unordered_map<char,long long int> freq;
vector<int> freq(26, 0);
long long int n = s.size();
long long int j = 0;// pointer to the starting of sliding window
long long int cnt1 = 0;//count of substring with atmost k distinct characters
long long int distinct = 0;
for(long long int i=0;i<n; i++){
freq[s[i] - 'a']++;
if(freq[s[i] - 'a'] == 1)distinct++;
while(distinct > k and j <= i){// decrease window size by deleting elements
// from the start of the window -> to make atmost k distict characters
freq[s[j]- 'a']--;
if(freq[s[j] - 'a'] == 0)distinct--;
j++;
}
cnt1 += i - j + 1;
}
return cnt1;
}
long long int substrCount (string s, int k) {
// number of substrings with exactly k distinct characters =
// number of substrings with atmost k distinct characters - number of substrings
// with atmost k - 1 distinct characters
// How to calculate the number of strings with atmost k distinct characters?
// -> Hashmap (to check distinctness) + Sliding window -->> TLE(it should work as well)
// -> freq[26] + Sliding window -->> AC
return helper(s , k ) - helper(s, k -1);
}
};
int main()
{
int t; cin >> t;
while (t--)
{
string s; cin >> s;
int k; cin >> k;
Solution ob;
cout <<ob.substrCount (s, k) << endl;
}