Skip to content

Commit 56ec4b9

Browse files
committed
2131. Longest Palindrome by Concatenating Two Letter Words: RETRY
1 parent 1d440d2 commit 56ec4b9

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,3 +1609,4 @@ mod s2126_destroying_asteroids;
16091609
mod s2127_maximum_employees_to_be_invited_to_a_meeting;
16101610
mod s2129_capitalize_the_title;
16111611
mod s2130_maximum_twin_sum_of_a_linked_list;
1612+
mod s2131_longest_palindrome_by_concatenating_two_letter_words;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* [2131] Longest Palindrome by Concatenating Two Letter Words
3+
*
4+
* You are given an array of strings words. Each element of words consists of two lowercase English letters.
5+
* Create the longest possible palindrome by selecting some elements from words and concatenating them in any order. Each element can be selected at most once.
6+
* Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return 0.
7+
* A palindrome is a string that reads the same forward and backward.
8+
*
9+
* Example 1:
10+
*
11+
* Input: words = ["lc","cl","gg"]
12+
* Output: 6
13+
* Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
14+
* Note that "clgglc" is another longest palindrome that can be created.
15+
*
16+
* Example 2:
17+
*
18+
* Input: words = ["ab","ty","yt","lc","cl","ab"]
19+
* Output: 8
20+
* Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
21+
* Note that "lcyttycl" is another longest palindrome that can be created.
22+
*
23+
* Example 3:
24+
*
25+
* Input: words = ["cc","ll","xx"]
26+
* Output: 2
27+
* Explanation: One longest palindrome is "cc", of length 2.
28+
* Note that "ll" is another longest palindrome that can be created, and so is "xx".
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= words.length <= 10^5
34+
* words[i].length == 2
35+
* words[i] consists of lowercase English letters.
36+
*
37+
*/
38+
pub struct Solution {}
39+
40+
// problem: https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/
41+
// discuss: https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/discuss/?currentPage=1&orderBy=most_votes&query=
42+
43+
// submission codes start here
44+
45+
impl Solution {
46+
pub fn longest_palindrome(words: Vec<String>) -> i32 {
47+
0
48+
}
49+
}
50+
51+
// submission codes end
52+
53+
#[cfg(test)]
54+
mod tests {
55+
use super::*;
56+
57+
#[test]
58+
#[ignore]
59+
fn test_2131_example_1() {
60+
let words = vec_string!["lc", "cl", "gg"];
61+
62+
let result = 6;
63+
64+
assert_eq!(Solution::longest_palindrome(words), result);
65+
}
66+
67+
#[test]
68+
#[ignore]
69+
fn test_2131_example_2() {
70+
let words = vec_string!["ab", "ty", "yt", "lc", "cl", "ab"];
71+
72+
let result = 8;
73+
74+
assert_eq!(Solution::longest_palindrome(words), result);
75+
}
76+
77+
#[test]
78+
#[ignore]
79+
fn test_2131_example_3() {
80+
let words = vec_string!["cc", "ll", "xx"];
81+
82+
let result = 2;
83+
84+
assert_eq!(Solution::longest_palindrome(words), result);
85+
}
86+
}

0 commit comments

Comments
 (0)