Skip to content

Commit 29548fc

Browse files
authored
Create 2981. Find Longest Special Substring That Occurs Thrice I (#657)
2 parents edc31d0 + b85bee2 commit 29548fc

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
int maximumLength(string s) {
4+
map<char,int>maps;
5+
for( auto &a:s){
6+
maps[a]++;
7+
}
8+
bool test=false;
9+
for(auto &a:maps){
10+
if(a.second>1)
11+
test=true;
12+
13+
}
14+
if(test==false)
15+
return -1;
16+
int res=-1;
17+
map<string,int>tmp;
18+
for(int i=0;i<s.size();i++){
19+
int j=i+1;
20+
tmp[s.substr(i,1)]++;
21+
while(j<s.size() && s[i]==s[j] ){
22+
tmp[s.substr(i,j-i+1)]++;
23+
j++;
24+
25+
}
26+
}
27+
for(auto &a:tmp){
28+
if(a.second>=3){
29+
int i=a.first.size();
30+
if(i>res){
31+
res=a.first.size();
32+
}
33+
}
34+
}
35+
return res;
36+
}
37+
};

0 commit comments

Comments
 (0)