Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Palindrome Pairs

Updated
2 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "336" 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 a 0-indexed array of unique strings words. A palindrome pair is a pair of integers (i, j) such that: 0 <= i, j < words.length, i != j, and words[i] + words[j] (the concatenation of the two strings) is a palindrome. Return an array of all the palindrome pairs of words. You must write an algorithm with O(sum of words[i].length) runtime complexity. Example 1: Input: words = ["abcd","dcba","lls","s","sssll"] Output: [[0,1],[1,0],[3,2],[2,4]] Explanation: The palindromes are ["abcddcba","dcbaabcd","slls","llssssll"] Example 2: Input: words = ["bat","tab","cat"] Output: [[0,1],[1,0]] Explanation: The palindromes are ["battab","tabbat"] Example 3: Input: words = ["a",""] Output: [[0,1],[1,0]] Explanation: The palindromes are ["a","a"] Constraints: 1 <= words.length <= 5000 0 <= words[i].length <= 300 words[i] consists of lowercase English letters.

Explanation

Here's an efficient solution to find palindrome pairs with O(sum of words[i].length) runtime complexity:

  • Hashing for Efficient Lookup: We'll use a hash map (dictionary in Python) to store each word and its index. This enables O(1) lookups for words during the palindrome pair search.
  • Iterate and Check Prefixes/Suffixes: For each word, we iterate through all possible prefixes and suffixes. If a prefix is a palindrome, we check if the reversed suffix exists in the hash map. Similarly, if a suffix is a palindrome, we check if the reversed prefix exists.
  • Handle Empty String: The empty string case needs special handling to avoid duplicate pairs.

  • Runtime Complexity: O(sum of words[i].length). Storage Complexity: O(sum of words[i].length)

Code

    def is_palindrome(s):
    return s == s[::-1]

def palindromePairs(words):
    word_map = {word: i for i, word in enumerate(words)}
    result = []

    for i, word in enumerate(words):
        n = len(word)
        for j in range(n + 1):
            prefix = word[:j]
            suffix = word[j:]

            if is_palindrome(prefix):
                reversed_suffix = suffix[::-1]
                if reversed_suffix in word_map and word_map[reversed_suffix] != i:
                    result.append([word_map[reversed_suffix], i])

            if j > 0 and is_palindrome(suffix):  # Prevent duplicate check for empty prefix
                reversed_prefix = prefix[::-1]
                if reversed_prefix in word_map and word_map[reversed_prefix] != i:
                    result.append([i, word_map[reversed_prefix]])

    return result

More from this blog

C

Chatmagic blog

2894 posts