-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006.Special_substring.cpp
More file actions
33 lines (33 loc) · 1.26 KB
/
Copy path006.Special_substring.cpp
File metadata and controls
33 lines (33 loc) · 1.26 KB
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
//Special Substring - codechef (1400)
//https://www.codechef.com/practice/course/two-pointers/SLIDINGWINDO/problems/SLDW0104
/*
Chef gave you a string S of size N which contains only lowercase English letters and asked you to find the length of the longest substring of the string which satisfies the following condition:
Each character β should appear at most f(β) times.
Here f(β) denotes the index of the character β in the alphabet series. For example f( ′a′) = 1, f( ′b′ ) = 2 and so on.
Note: A substring of a string is a contiguous subsequence of that string.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
int left = 0, maxi = 0;
vector <int> freq(26, 0);
//taken only 26, as they have mentioned that the string have only lowercase
for (int right = 0; right < n; right++) {
freq[s[right] - 'a']++;
while (freq[s[right] - 'a'] > (s[right] - 'a' + 1)) {
freq[s[left] - 'a']--;
left++;
}
//update the maximum length of the special substring
maxi = max(maxi, right - left + 1);
}
cout << maxi << endl;
}
}