2131. Longest Palindrome by Concatenating Two Letter Words

Medium

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.

解題

Runtime: 251 ms, faster than 91.47%

Memory Usage: 12.6 MB, less than 97.67%

func longestPalindrome(words []string) int {
    pair, solo := 0, 0
    m := make(map[string]int)
    
    for _, word := range words {
        m[word]++
        
        if word[0] == word[1] {
            if m[word]>=2 && m[word] % 2 == 0 {
                pair++
                solo--
            } else {
                solo++
            }
            continue
        }
        
        reverse := string([]byte{word[1], word[0]})
        if m[word] <= m[reverse] {
            pair++
        }
    }
    
    if solo != 0 {
        return pair*4 + 2
    }
    
    return pair*4
}

Last updated