Skip to content

Commit 8963e25

Browse files
committed
1 parent ee0f6dd commit 8963e25

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,3 +1335,4 @@ mod s1766_tree_of_coprimes;
13351335
mod s1768_merge_strings_alternately;
13361336
mod s1769_minimum_number_of_operations_to_move_all_balls_to_each_box;
13371337
mod s1770_maximum_score_from_performing_multiplication_operations;
1338+
mod s1771_maximize_palindrome_length_from_subsequences;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* [1771] Maximize Palindrome Length From Subsequences
3+
*
4+
* You are given two strings, word1 and word2. You want to construct a string in the following manner:
5+
*
6+
* Choose some non-empty subsequence subsequence1 from word1.
7+
* Choose some non-empty subsequence subsequence2 from word2.
8+
* Concatenate the subsequences: subsequence1 + subsequence2, to make the string.
9+
*
10+
* Return the length of the longest palindrome that can be constructed in the described manner. If no palindromes can be constructed, return 0.
11+
* 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.
12+
* A palindrome is a string that reads the same forward as well as backward.
13+
*
14+
* Example 1:
15+
*
16+
* Input: word1 = "cacb", word2 = "cbba"
17+
* Output: 5
18+
* Explanation: Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.
19+
* Example 2:
20+
*
21+
* Input: word1 = "ab", word2 = "ab"
22+
* Output: 3
23+
* Explanation: Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.
24+
* Example 3:
25+
*
26+
* Input: word1 = "aa", word2 = "bb"
27+
* Output: 0
28+
* Explanation: You cannot construct a palindrome from the described method, so return 0.
29+
*
30+
* Constraints:
31+
*
32+
* 1 <= word1.length, word2.length <= 1000
33+
* word1 and word2 consist of lowercase English letters.
34+
*
35+
*/
36+
pub struct Solution {}
37+
38+
// problem: https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/
39+
// discuss: https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/discuss/?currentPage=1&orderBy=most_votes&query=
40+
41+
// submission codes start here
42+
43+
impl Solution {
44+
// Credit: https://leetcode.com/problems/maximize-palindrome-length-from-subsequences/solutions/3213834/just-a-runnable-solution/
45+
pub fn longest_palindrome(word1: String, word2: String) -> i32 {
46+
let mut dp = vec![vec![-1; 2002]; 2002];
47+
let s = word1.clone() + &word2;
48+
let s = s.chars().collect::<Vec<char>>();
49+
let n = s.len();
50+
Solution::lcs(&s, 0, n - 1, &mut dp);
51+
let mut mp = std::collections::HashMap::new();
52+
for (i, c) in word1.chars().enumerate() {
53+
mp.entry(c).or_insert(i as i32);
54+
}
55+
let mut result = 0;
56+
57+
let mut m = std::collections::HashMap::new();
58+
let word2 = word2.chars().rev().collect::<Vec<_>>();
59+
60+
for (i, c) in word2.iter().enumerate() {
61+
if mp.contains_key(&c) && !m.contains_key(&c) {
62+
m.insert(c, true);
63+
let id1: i32 = mp[&c] + 1;
64+
let id2 = n as i32 - i as i32 - 2;
65+
if id1 >= 0 && id2 >= 0 {
66+
result = result.max(2 + dp[id1 as usize][id2 as usize]);
67+
}
68+
}
69+
}
70+
71+
result
72+
}
73+
74+
fn lcs(s: &[char], i: usize, j: usize, dp: &mut Vec<Vec<i32>>) -> i32 {
75+
if i > j {
76+
dp[i][j] = 0;
77+
return 0;
78+
}
79+
if i == j {
80+
dp[i][j] = 1;
81+
return 1;
82+
}
83+
if dp[i][j] != -1 {
84+
return dp[i][j];
85+
}
86+
if s[i] == s[j] {
87+
let v = 2 + Solution::lcs(s, i + 1, j - 1, dp);
88+
dp[i][j] = v;
89+
return v;
90+
}
91+
let v1 = Solution::lcs(s, i + 1, j, dp);
92+
let v2 = Solution::lcs(s, i, j - 1, dp);
93+
dp[i][j] = v1.max(v2);
94+
dp[i][j]
95+
}
96+
}
97+
98+
// submission codes end
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
104+
#[test]
105+
fn test_1771_example_1() {
106+
let word1 = "cacb".to_string();
107+
let word2 = "cbba".to_string();
108+
109+
let result = 5;
110+
111+
assert_eq!(Solution::longest_palindrome(word1, word2), result);
112+
}
113+
114+
#[test]
115+
fn test_1771_example_2() {
116+
let word1 = "ab".to_string();
117+
let word2 = "ab".to_string();
118+
119+
let result = 3;
120+
121+
assert_eq!(Solution::longest_palindrome(word1, word2), result);
122+
}
123+
124+
#[test]
125+
fn test_1771_example_3() {
126+
let word1 = "aa".to_string();
127+
let word2 = "bb".to_string();
128+
129+
let result = 0;
130+
131+
assert_eq!(Solution::longest_palindrome(word1, word2), result);
132+
}
133+
}

0 commit comments

Comments
 (0)