|
| 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