2131. Longest Palindrome by Concatenating Two Letter Words Medium

@problem@discussion
#Array#Hash Table#String#Greedy#Counting



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 */
38pub 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
45impl Solution {
46    pub fn longest_palindrome(words: Vec<String>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_2131() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.