-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.longest-palindromic-substring.cpp
57 lines (53 loc) · 1.57 KB
/
5.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* @lc app=leetcode id=5 lang=cpp
*
* [5] Longest Palindromic Substring
*/
// @lc code=start
class Solution {
public:
short len;
string longestPalindrome(string s) {
len = s.length();
string oddstr = odd(s);
string evenstr = even(s);
cout << oddstr << endl << evenstr << endl;
return (oddstr.length() > evenstr.length()) ? oddstr : evenstr;
}
string odd(string s) {
short start = 0, end = 0;
for (short i = 0; i < len; i++) {
short temps = i, tempe = i;
while (temps - 1 >= 0 && tempe + 1 < len) {
if (s[temps - 1] == s[tempe + 1]) {
temps--;
tempe++;
} else break;
}
if (tempe - temps + 1 > end - start + 1) {
start = temps;
end = tempe;
}
}
return s.substr(start, end - start + 1);
}
string even(string s) {
short start = 0, end = 0;
for (short i = 0; i < len - 1; i++) {
short temps = i + 1, tempe = i;
while (temps - 1 >= 0 && tempe + 1 < len) {
if (s[temps - 1] == s[tempe + 1]) {
temps--;
tempe++;
} else break;
}
if (temps < tempe && tempe - temps + 1 > end - start + 1) {
start = temps;
end = tempe;
}
}
cout << start << end << endl;
return (start == end) ? "" : s.substr(start, end - start + 1);
}
};
// @lc code=end