- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cpp
More file actions
Latest commit
25 lines (23 loc) · 701 Bytes
/
Copy pathSolution.cpp
File metadata and controls
25 lines (23 loc) · 701 Bytes
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
classSolution {
public:
int maxLen;
int start;
string longestPalindrome(string s) {
if (s.size()<2) return s;
for(int i=0;i<s.size()-1;i++) {
extendPalindrom(s, i, i); //assume odd lenght, try to extend as long as possible
extendPalindrom(s, i, i+1); //assume even lenght
}
return s.substr(start, maxLen);
}
voidextendPalindrom(string str, int s, int e) {
while(s>=0 && e<str.size() && str[s]==str[e]) {
s--;
e++;
}
if(maxLen<e-s-1) { //we have e+1 after prev increment
start=s+1; //we have s-1 after prev decrement
maxLen=e-s-1;
}
}
};