-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_LongestPalindromicSubstring
49 lines (40 loc) · 1.35 KB
/
5_LongestPalindromicSubstring
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
class Solution {
public:
string longestPalindrome(string s) {
//use dynamic programming table
bool dp[s.length()][s.length()];
//initialize to 0
memset(dp, 0, sizeof(dp));
int longestPalindromLength = 1;
int start = 0;
//set the values of all substrings of length 1 (the numbers on the diagonal) to True in dp table
for (int i = 0; i < s.length(); i++){
dp[i][i] = true;
}
//try to find palindroms of length 2
for (int j = 0; j < s.length() - 1; j++){
if (s[j] == s[j + 1]){
dp[j][j + 1] = true;
start = j;
longestPalindromLength = 2;
}
}
//now check palindroms 3 and longer
for (int k = 3; k <= s.length(); ++k) {
for (int i = 0; i < s.length() - k + 1; ++i) {
//get end index
int j = i + k - 1;
//check if substring from start to j (end) is a palindrom
if (dp[i + 1][j - 1] && s[i] == s[j]) {
dp[i][j] = true;
//if a longer palindrom has been found, update longest length
if (k > longestPalindromLength) {
start = i;
longestPalindromLength = k;
}
}
}
}
return s.substr(start, longestPalindromLength);
}
};