-
Notifications
You must be signed in to change notification settings - Fork 118
/
5.cpp
50 lines (44 loc) · 1.2 KB
/
5.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
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int n = s.length();
int start = 0, maxLen = 1; // important, cos a single letter is palindrome
int i, j, k ;
for (i = 0; i < n; i++) {
j = i, k = i + 1; //even length
while (j >= 0 && k < n) {
if (s[j] != s[k]) break;
if (k - j + 1 > maxLen) {
start = j;
maxLen = k - j + 1;
}
j--;
k++;
}
j = i - 1, k = i + 1; // odd length
while(j >= 0 && k <= n - 1) {
if (s[j] != s[k]) break;
if (k - j + 1 > maxLen) {
start = j;
maxLen = k - j + 1;
}
j--;
k++;
}
}
return s.substr(start, maxLen);
}
};
int main() {
string str0 = "aabac";
string str1 = "a";
Solution s;
assert(s.longestPalindrome(str0) == "aba");
assert(s.longestPalindrome(str1) == "a");
printf("all tests passed!\n");
return 0;
}