[LeetCode] 2131. Longest Palindrome by Concatenating Two Letter Words

You are given an array of strings words. Each element of words consists of two lowercase English letters.

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.

Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return 0.

A palindrome is a string that reads the same forward and backward.

Example 1:

Input: words = ["lc","cl","gg"]
Output: 6
Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
Note that "clgglc" is another longest palindrome that can be created.

Example 2:

Input: words = ["ab","ty","yt","lc","cl","ab"]
Output: 8
Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
Note that "lcyttycl" is another longest palindrome that can be created.

Example 3:

Input: words = ["cc","ll","xx"]
Output: 2
Explanation: One longest palindrome is "cc", of length 2.
Note that "ll" is another longest palindrome that can be created, and so is "xx".

Constraints:

  • 1 <= words.length <= 105
  • words[i].length == 2
  • words[i] consists of lowercase English letters.

连接两字母单词得到的最长回文串。

给你一个字符串数组 words 。words 中每个元素都是一个包含 两个 小写英文字母的单词。

请你从 words 中选择一些元素并按 任意顺序 连接它们,并得到一个 尽可能长的回文串 。每个元素 至多 只能使用一次。

请你返回你能得到的最长回文串的 长度 。如果没办法得到任何一个回文串,请你返回 0 。

回文串 指的是从前往后和从后往前读一样的字符串。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-palindrome-by-concatenating-two-letter-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题不难,不过比一般的回文串的题稍微难一些,我直接介绍我的做法。

题号都到 2100+ 了,我们对回文串的定义应该是很熟悉了,不是 aba 类型,就是 abba 类型。所以如果想要拼接成一个尽量长的回文串,那么比如当我们找到一个单词形如 ab 的时候,我们也要去 input 里寻找我们是否有一个形如 ba 的单词。如果有,那么我们能拼接出来的长度就是 ab 出现次数和 ba 出现次数的较小值 * 2。题目中也有可能给出 aa 形式的单词,遇到这种单词,我们也可以像遇到 ab 类型的单词一样处理,因为如果我们有多个 aa,那么我们就可以把单词 aa 当做回文串的非中间部分来用,比如 aaxxxxxaa 这种形式。只有我们遇到一个类似 aa 的形式,但是只出现过一次的单词,才可以把他当做 aba 类型的回文串中的 b 使用。

具体的实现方法上,我们还是需要用一个 hashmap 记录不同的单词和他们各自出现的次数。遍历所有的单词,对于当前单词而言,

  • 如果他的 reverse 存在于 hashmap,那么当前单词和他的reverse就能为最后的回文串贡献 4 个长度
  • 如果他的reverse 不存在于 hashmap,也没问题,暂时先把当前单词和次数存入 hashmap

最后再遍历一次 hashmap,如果找到一个形如 aa 的单词并且他的出现次数只有 1 次的时候,这个单词可以为最后的回文串贡献 2 个长度

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int longestPalindrome(String[] words) {
 3         HashMap<String, Integer> map = new HashMap<>();
 4         int max = 0;
 5         for (int i = 0; i < words.length; i++) {
 6             String reverse = new StringBuilder(words[i]).reverse().toString();
 7             if (map.containsKey(reverse)) {
 8                 max += 4;
 9                 map.put(reverse, map.get(reverse) - 1);
10                 if (map.get(reverse) == 0) {
11                     map.remove(reverse);
12                 }
13             } else {
14                 map.put(words[i], map.getOrDefault(words[i], 0) + 1);
15             }
16         }
17 
18         for (String key : map.keySet()) {
19             if (map.get(key) == 1 && (key.charAt(0) == key.charAt(1))) {
20                 max += 2;
21                 break;
22             }
23         }
24         return max;
25     }
26 }

 

LeetCode 题目总结

posted @ 2022-11-05 05:44  CNoodle  阅读(96)  评论(0编辑  收藏  举报