Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Longest Palindrome by Concatenating Two Letter Words

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2131" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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.

Explanation

Here's the breakdown of the approach, complexities, and the Python code:

  • High-Level Approach:

    • Count the occurrences of each word.
    • Iterate through the word counts, pairing words with their reverses to maximize palindrome length.
    • Handle cases where a word is its own reverse (e.g., "gg", "cc"), using at most one such word in the middle of the palindrome.
  • Complexity:

    • Runtime Complexity: O(N), where N is the number of words.
    • Storage Complexity: O(1), as the number of distinct words is limited to 26 * 26.

Code

    def longestPalindrome(words):
    """
    Finds the length of the longest possible palindrome that can be created
    by selecting some elements from words and concatenating them.

    Args:
        words: A list of strings, where each string has length 2.

    Returns:
        The length of the longest palindrome that can be created.
    """

    counts = {}
    for word in words:
        counts[word] = counts.get(word, 0) + 1

    length = 0
    central = False

    for word, count in counts.items():
        reversed_word = word[::-1]

        if word == reversed_word:
            if count % 2 == 0:
                length += count
            else:
                length += count - 1
                central = True
        elif word < reversed_word and reversed_word in counts:
            length += 2 * min(count, counts[reversed_word])

    if central:
        length += 1

    return 2 * length

More from this blog

C

Chatmagic blog

2894 posts