-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_073.cpp
62 lines (56 loc) · 1.27 KB
/
Day_073.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
/**
*Problem Statement: A string is called boring if all the characters of the string are same.You are given a string
S of length N, consisting of lowercase english alphabets. Find the length of the longest boring substring of S
which occurs more than once.Note that if there is no boring substring which occurs more than once in S,
the answer will be 00.A substring is obtained by deleting some (possibly zero) elements from the beginning
of the string and some (possibly zero) elements from the end of the string.
*Author: Kunal Kathpal (https://github.com/kunal-2002)
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
cout<<"Enter number of test cases:\t";
int T;
cin>>T;
while(T--){
int n;
cin>>n;
string str;
cin>>str;
string res = "";
unordered_set<string> s;
res = str[0];
int I, mx, sum;
I = mx = 0;
sum = 1;
for(int i=1; i<n; i++){
if(str[i-1]!=str[i]){
if(mx<sum){
mx = sum;
mx--;
}
if(s.find(res) != s.end())
I = max(I, sum);
s.insert(res);
res = str[i];
sum = 1;
}
else{
sum++;
res+=str[i];
}
}
if(s.find(res)!=s.end()){
I = max(I, sum);
}
else{
if(mx<sum){
mx = sum;
mx--;
}
}
int ans = max(I,mx);
cout<<ans<<endl;
}
return 0;
}