返回介绍

solution / 2100-2199 / 2131.Longest Palindrome by Concatenating Two Letter Words / README_EN

发布于 2024-06-17 01:03:09 字数 4695 浏览 0 评论 0 收藏 0

2131. Longest Palindrome by Concatenating Two Letter Words

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def longestPalindrome(self, words: List[str]) -> int:
    cnt = Counter(words)
    ans = x = 0
    for k, v in cnt.items():
      if k[0] == k[1]:
        x += v & 1
        ans += v // 2 * 2 * 2
      else:
        ans += min(v, cnt[k[::-1]]) * 2
    ans += 2 if x else 0
    return ans
class Solution {
  public int longestPalindrome(String[] words) {
    Map<String, Integer> cnt = new HashMap<>();
    for (var w : words) {
      cnt.put(w, cnt.getOrDefault(w, 0) + 1);
    }
    int ans = 0, x = 0;
    for (var e : cnt.entrySet()) {
      var k = e.getKey();
      var rk = new StringBuilder(k).reverse().toString();
      int v = e.getValue();
      if (k.charAt(0) == k.charAt(1)) {
        x += v & 1;
        ans += v / 2 * 2 * 2;
      } else {
        ans += Math.min(v, cnt.getOrDefault(rk, 0)) * 2;
      }
    }
    ans += x > 0 ? 2 : 0;
    return ans;
  }
}
class Solution {
public:
  int longestPalindrome(vector<string>& words) {
    unordered_map<string, int> cnt;
    for (auto& w : words) cnt[w]++;
    int ans = 0, x = 0;
    for (auto& [k, v] : cnt) {
      string rk = k;
      reverse(rk.begin(), rk.end());
      if (k[0] == k[1]) {
        x += v & 1;
        ans += v / 2 * 2 * 2;
      } else if (cnt.count(rk)) {
        ans += min(v, cnt[rk]) * 2;
      }
    }
    ans += x ? 2 : 0;
    return ans;
  }
};
func longestPalindrome(words []string) int {
  cnt := map[string]int{}
  for _, w := range words {
    cnt[w]++
  }
  ans, x := 0, 0
  for k, v := range cnt {
    if k[0] == k[1] {
      x += v & 1
      ans += v / 2 * 2 * 2
    } else {
      rk := string([]byte{k[1], k[0]})
      if y, ok := cnt[rk]; ok {
        ans += min(v, y) * 2
      }
    }
  }
  if x > 0 {
    ans += 2
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文