Skip to content

Commit 3e5e2e9

Browse files
committed
New Problem Solution -"Maximize Palindrome Length From Subsequences"
1 parent 2ed675b commit 3e5e2e9

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ LeetCode
3232
|1775|[Equal Sum Arrays With Minimum Number of Operations](https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [C++](./algorithms/cpp/equalSumArraysWithMinimumNumberOfOperations/EqualSumArraysWithMinimumNumberOfOperations.cpp)|Medium|
3333
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/) | [C++](./algorithms/cpp/closestDessertCost/ClosestDessertCost.cpp)|Medium|
3434
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [C++](./algorithms/cpp/countItemsMatchingARule/CountItemsMatchingARule.cpp)|Easy|
35+
|1771|[Maximize Palindrome Length From Subsequences](https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/) | [C++](./algorithms/cpp/maximizePalindromeLengthFromSubsequences/MaximizePalindromeLengthFromSubsequences.cpp)|Hard|
3536
|1769|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C++](./algorithms/cpp/minimumNumberOfOperationsToMoveAllBallsToEachBox/MinimumNumberOfOperationsToMoveAllBallsToEachBox.cpp)|Medium|
3637
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [C++](./algorithms/cpp/mergeStringsAlternately/MergeStringsAlternately.cpp)|Easy|
3738
|1765|[Map of Highest Peak](https://leetcode.com/problems/map-of-highest-peak/) | [C++](./algorithms/cpp/mapOfHighestPeak/MapOfHighestPeak.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Source : https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/
2+
// Author : Hao Chen
3+
// Date : 2021-03-27
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given two strings, word1 and word2. You want to construct a string in the following manner:
8+
*
9+
* Choose some non-empty subsequence subsequence1 from word1.
10+
* Choose some non-empty subsequence subsequence2 from word2.
11+
* Concatenate the subsequences: subsequence1 + subsequence2, to make the string.
12+
*
13+
* Return the length of the longest palindrome that can be constructed in the described manner. If no
14+
* palindromes can be constructed, return 0.
15+
*
16+
* A subsequence of a string s is a string that can be made by deleting some (possibly none)
17+
* characters from s without changing the order of the remaining characters.
18+
*
19+
* A palindrome is a string that reads the same forward as well as backward.
20+
*
21+
* Example 1:
22+
*
23+
* Input: word1 = "cacb", word2 = "cbba"
24+
* Output: 5
25+
* Explanation: Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.
26+
*
27+
* Example 2:
28+
*
29+
* Input: word1 = "ab", word2 = "ab"
30+
* Output: 3
31+
* Explanation: Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.
32+
*
33+
* Example 3:
34+
*
35+
* Input: word1 = "aa", word2 = "bb"
36+
* Output: 0
37+
* Explanation: You cannot construct a palindrome from the described method, so return 0.
38+
*
39+
* Constraints:
40+
*
41+
* 1 <= word1.length, word2.length <= 1000
42+
* word1 and word2 consist of lowercase English letters.
43+
******************************************************************************************************/
44+
45+
/*
46+
// The basic algorthim come from
47+
// https://leetcode.com/problems/longest-palindromic-subsequence/
48+
49+
int longestPalindromeSubseq(string& s) {
50+
int n = s.size();
51+
vector<vector<int>> dp(n, vector<int>(n, 0));
52+
53+
for (int start = n-1; start>=0; start--) {
54+
for (int end = start ; end < n ; end++) {
55+
if (start == end) {
56+
dp[start][end] = 1;
57+
continue;
58+
}
59+
if (s[start] == s[end]) {
60+
dp[start][end] = dp[start+1][end-1] + 2;
61+
}else{
62+
dp[start][end] = max (dp[start+1][end], dp[start][end-1]);
63+
}
64+
65+
}
66+
}
67+
return dp[0][n-1];
68+
}
69+
*/
70+
71+
72+
class Solution {
73+
74+
public:
75+
int longestPalindrome(string word1, string word2) {
76+
77+
string s = word1 + word2;
78+
int n = s.size();
79+
vector<vector<int>> dp(n, vector<int>(n, 0));
80+
81+
int result = 0;
82+
for (int start = n-1; start>=0; start--) {
83+
for (int end = start ; end < n ; end++) {
84+
if (start == end) {
85+
dp[start][end] = 1;
86+
continue;
87+
}
88+
if (s[start] == s[end]) {
89+
dp[start][end] = dp[start+1][end-1] + 2;
90+
// <----------- different ----------->
91+
//only consider when `start` and `end` in different string.
92+
if (start < word1.size() && end >= word1.size()){
93+
result = max(result, dp[start][end]);
94+
}
95+
// <----------- different ----------->
96+
}else{
97+
dp[start][end] = max (dp[start+1][end], dp[start][end-1]);
98+
}
99+
100+
}
101+
}
102+
return result;
103+
}
104+
};

0 commit comments

Comments
 (0)