forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0005.cpp
More file actions
40 lines (33 loc) · 952 Bytes
/
Copy path0005.cpp
File metadata and controls
40 lines (33 loc) · 952 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
string longestPalindrome(string s) {
if(s.length() <= 1)
return s;
int max_len = 1, st = 0, end = 0;
int siz = s.length();
for(int i=0; i<siz-1; i++){
cal(i, i, siz, max_len, st, end, s);
}
for(int i=0; i<siz-1; i++){
cal(i, i+1, siz, max_len, st, end, s);
}
return s.substr(st, max_len);
}
//solved using recursion, not the best way probably
void cal(int l, int r, int siz, int &max_len, int &st, int &end, string s){
while(l >= 0 && r < siz){
if(s[l] == s[r]){
l--;
r++;
}
else
break;
}
int len = r - l - 1;
if(len > max_len){
max_len = len;
st = l + 1;
end = r - 1;
}
}
};