-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path005.cpp
52 lines (49 loc) · 1.48 KB
/
005.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int max = 1;
int first = 0; // index of the first letter
int len = s.size();
if (len <= 1) {
return s;
}
for (int center = 0; center < len; center++) {
// explore every letter a center node of a palindrom
int l = 0;
bool palindrom = true;
while (palindrom and l < center and center + l + 1 < len) {
if (s[center - l - 1] != s[center + l + 1]) {
palindrom = false;
} else {
l++;
}
}
int max_candidate = 2 * l + 1;
if (max_candidate >= max) {
max = max_candidate;
first = center - l;
}
// explore every letter a left-to-center node of a palindrom
l = 0;
palindrom = true;
while (palindrom and l <= center and center + 1 + l < len) {
if (s[center - l] != s[center + 1 + l]) {
palindrom = false;
} else {
l++;
}
}
max_candidate = 2 * l;
if (max_candidate >= max) {
max = max_candidate;
first = center - l + 1;
}
}
return s.substr(first, max);
}
};
int main() {
return 0;
}