-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest2char-sub-string.cpp
More file actions
46 lines (43 loc) · 1.16 KB
/
Longest2char-sub-string.cpp
File metadata and controls
46 lines (43 loc) · 1.16 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
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<string>
using namespace std;
string longest(string input){
int start,charAtIndex,substrLength,maxSubstrLength;
char charA,charB;
bool dif;
string substr;
maxSubstrLength=0;
for(start=0;start<input.length()-2;start++){
charAtIndex=start;
substrLength=2;
dif=true;
charA=input[charAtIndex];
charAtIndex++;
while(input[charAtIndex]==charA){
charAtIndex++;
substrLength++;
}
if(charAtIndex<input.length()){
charB=input[charAtIndex];
charAtIndex++;
while(dif){
if(input[charAtIndex]==charA || input[charAtIndex]==charB) substrLength++;
else dif=false;
charAtIndex++;
}
}
if(substrLength>maxSubstrLength){
maxSubstrLength=substrLength;
substr=input.substr(start,substrLength);
}
if(maxSubstrLength>(input.length()-(start+1))) break;
}
return substr;
}
int main(){
string input;
cin>>input;
cout<<longest(input)<<endl;
system("PAUSE");
return 0;
}