Skip to content

Commit

Permalink
New Problem Solution -"Maximize Palindrome Length From Subsequences"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Mar 27, 2021
1 parent 2ed675b commit 3e5e2e9
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LeetCode
|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|
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/) | [C++](./algorithms/cpp/closestDessertCost/ClosestDessertCost.cpp)|Medium|
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [C++](./algorithms/cpp/countItemsMatchingARule/CountItemsMatchingARule.cpp)|Easy|
|1771|[Maximize Palindrome Length From Subsequences](https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/) | [C++](./algorithms/cpp/maximizePalindromeLengthFromSubsequences/MaximizePalindromeLengthFromSubsequences.cpp)|Hard|
|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|
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [C++](./algorithms/cpp/mergeStringsAlternately/MergeStringsAlternately.cpp)|Easy|
|1765|[Map of Highest Peak](https://leetcode.com/problems/map-of-highest-peak/) | [C++](./algorithms/cpp/mapOfHighestPeak/MapOfHighestPeak.cpp)|Medium|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Source : https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/
// Author : Hao Chen
// Date : 2021-03-27

/*****************************************************************************************************
*
* You are given two strings, word1 and word2. You want to construct a string in the following manner:
*
* Choose some non-empty subsequence subsequence1 from word1.
* Choose some non-empty subsequence subsequence2 from word2.
* Concatenate the subsequences: subsequence1 + subsequence2, to make the string.
*
* Return the length of the longest palindrome that can be constructed in the described manner. If no
* palindromes can be constructed, return 0.
*
* A subsequence of a string s is a string that can be made by deleting some (possibly none)
* characters from s without changing the order of the remaining characters.
*
* A palindrome is a string that reads the same forward as well as backward.
*
* Example 1:
*
* Input: word1 = "cacb", word2 = "cbba"
* Output: 5
* Explanation: Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.
*
* Example 2:
*
* Input: word1 = "ab", word2 = "ab"
* Output: 3
* Explanation: Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.
*
* Example 3:
*
* Input: word1 = "aa", word2 = "bb"
* Output: 0
* Explanation: You cannot construct a palindrome from the described method, so return 0.
*
* Constraints:
*
* 1 <= word1.length, word2.length <= 1000
* word1 and word2 consist of lowercase English letters.
******************************************************************************************************/

/*
// The basic algorthim come from
// https://leetcode.com/problems/longest-palindromic-subsequence/
int longestPalindromeSubseq(string& s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int start = n-1; start>=0; start--) {
for (int end = start ; end < n ; end++) {
if (start == end) {
dp[start][end] = 1;
continue;
}
if (s[start] == s[end]) {
dp[start][end] = dp[start+1][end-1] + 2;
}else{
dp[start][end] = max (dp[start+1][end], dp[start][end-1]);
}
}
}
return dp[0][n-1];
}
*/


class Solution {

public:
int longestPalindrome(string word1, string word2) {

string s = word1 + word2;
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));

int result = 0;
for (int start = n-1; start>=0; start--) {
for (int end = start ; end < n ; end++) {
if (start == end) {
dp[start][end] = 1;
continue;
}
if (s[start] == s[end]) {
dp[start][end] = dp[start+1][end-1] + 2;
// <----------- different ----------->
//only consider when `start` and `end` in different string.
if (start < word1.size() && end >= word1.size()){
result = max(result, dp[start][end]);
}
// <----------- different ----------->
}else{
dp[start][end] = max (dp[start+1][end], dp[start][end-1]);
}

}
}
return result;
}
};

0 comments on commit 3e5e2e9

Please sign in to comment.