-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.longest-palindromic-substring.cpp
More file actions
46 lines (40 loc) · 1.09 KB
/
5.longest-palindromic-substring.cpp
File metadata and controls
46 lines (40 loc) · 1.09 KB
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
#include "testharness.h"
#include <map>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
class Solution {
public:
string longestPalindrome(string s) {
int size = s.size();
if (size < 2) return s;
int maxLength = 0;
int b = 0;
for (int n = 0; n < size; n++) {
for (int m = 0; m < 2; m++) {
int i = n;
int j = n + m;
while (i >= 0 && j < size) {
if (s[i] == s[j]) {
i--;
j++;
} else {
break;
}
}
int tmpLength = j - i - 1;
if (maxLength < tmpLength) {
b = i + 1;
maxLength = tmpLength;
}
}
}
return s.substr(b, maxLength);
}
};
TEST(Solution, test) {
ASSERT_EQ("", longestPalindrome(""));
ASSERT_EQ("aba", longestPalindrome("abaa"));
ASSERT_EQ("ababababababa", longestPalindrome("ababababababab"));
}