forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximize-palindrome-length-from-subsequences.cpp
54 lines (52 loc) · 1.61 KB
/
maximize-palindrome-length-from-subsequences.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
53
54
// Time: O((m + n)^2)
// Space: O((m + n)^2)
class Solution {
public:
int longestPalindrome(string word1, string word2) {
string s = word1 + word2;
vector<vector<int>> dp(size(s), vector<int>(size(s)));
int result = 0;
for (int j = 0; j < size(s); ++j) {
dp[j][j] = 1;
for (int i = j - 1; i >= 0; --i) {
if (s[i] == s[j]) {
dp[i][j] = (i + 1 == j) ? 2 : dp[i + 1][j - 1] + 2;
if (i < size(word1) && size(word1) <= j) {
result = max(result, dp[i][j]);
}
} else {
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
return result;
}
};
// Time: O((m + n)^2)
// Space: O((m + n)^2)
class Solution2 {
public:
int longestPalindrome(string word1, string word2) {
string s = word1 + word2;
vector<vector<int>> dp(size(s), vector<int>(size(s)));
for (int j = 0; j < size(s); ++j) {
dp[j][j] = 1;
for (int i = j - 1; i >= 0; --i) {
if (s[i] == s[j]) {
dp[i][j] = (i + 1 == j) ? 2 : dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
int result = 0;
for (int i = 0; i < size(word1); ++i) {
for (int j = size(word1); j < size(s); ++j) {
if (s[i] == s[j]) {
result = max(result, dp[i][j]);
}
}
}
return result;
}
};