-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_palindromic_substring2,cpp
32 lines (31 loc) · 1.1 KB
/
longest_palindromic_substring2,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
class solution{
public:
string LongestPalindromicsubstring(string s){
int start = 0;
int maxLen = 0;
for(int i=1;i<s.length(), i++){
//even
int l = i-1;
int r = i;
while(l>=0 && r<=s.length() && s[l] == s[r]){
if(r-l+1 > maxLen){
maxLen = r - l + 1;
start = l;
}
--l;
++r;
}
//odd
l = i-1;
r = i+1;
while(l>=0 && r<=s.length() && s[l] == s[r]){
if( r - l + 1 > maxLen){
maxLen = r - l + 1;
start = l;
}
--l;
++r;
}
} return s.substr(start, maxLen);
}
};