-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_longest_palindromic_substring.cpp
More file actions
57 lines (46 loc) · 2.05 KB
/
5_longest_palindromic_substring.cpp
File metadata and controls
57 lines (46 loc) · 2.05 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
47
48
49
50
51
52
53
54
55
56
57
/*
NOTE: this is dynamic programming solution with O(n^2) time and space
complexity. Acceptable given the problem constraints, but other
solutions also exists:
+---------------------------+------------------+------------------+
| Solution | Time Complexity | Space Complexity |
+---------------------------+------------------+------------------+
| Bruteforce | O(n^3) | O(1) |
+---------------------------+------------------+------------------+
| DP (2D table) | O(n^2) | O(n^2) |
+---------------------------+------------------+------------------+
| Expand-around-centers | O(n^2) | O(1) |
+---------------------------+------------------+------------------+
| Manacher's Algorithm | O(n) | O(n) |
+---------------------------+------------------+------------------+
*/
class Solution {
public:
string longestPalindrome(string s) {
size_t len = s.size();
vector<vector<bool>> palindromes(len, vector<bool>(len, false));
array<size_t, 2> longest = {0, 1};
// initialize 1-char solutions (always a palindrome)
for (size_t i = 0; i < len; ++i) {
palindromes[i][i] = true;
}
// initialize 2-char solutions
for (size_t i = 0; i < len - 1; ++i) {
if (s[i] == s[i + 1]) {
palindromes[i][i + 1] = true;
longest = {i, 2};
}
}
// verify recursively all the strings from length 3 to len
// using the DP matrix partial solutions
for (size_t span = 2; span < len; ++span) {
for (size_t i = 0; i < len - span; ++i) {
if (s[i] == s[i + span] && palindromes[i + 1][i + span - 1]) {
palindromes[i][i + span] = true;
longest = {i, span + 1};
}
}
}
return s.substr(longest[0], longest[1]);
}
};