-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlongest_palindromic_substring.cpp
43 lines (33 loc) · 1.26 KB
/
longest_palindromic_substring.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
33
34
35
36
37
38
39
40
41
42
43
class Solution {
private:
string helper(string s, int begin, int end) {
if(s.empty()) return NULL;
if(end >= s.size()) return "";
while((begin >= 0 and end <= s.length()-1) and (s[begin] == s[end])) {
begin--; end++;
}
return s.substr(begin+1, end - begin - 1); //length of current palindromic substr
}
public:
string longestPalindrome(string s) {
/*palindromic substrings can be either even length or odd length..
to find palindromes.. start from middle of substring and and expand the window on both the ends..
*/
if(s.length() == 1 or s == "") return s;
string curr = "";
//int maxLen = globalMax.length();
for(int i=0; i<s.size(); i++){
string tmp = helper(s, i, i); // for odd length strings..
//maxLen = max(maxLen, oddLength.length());
if(tmp.size() > curr.size()){
curr = tmp;
}
tmp = helper(s, i, i+1); // for even length strings
//maxLen = max(maxLen, evenLength.length());
if(tmp.size() > curr.size()){
curr = tmp;
}
}
return curr;
}
};