Advertisement

Palindrome Pairs - LeetCode 336 Solution

Palindrome Pairs - Complete Solution Guide

Palindrome Pairs is LeetCode problem 336, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 ["abcd

Detailed Explanation

The problem asks us to find all pairs of distinct indices (i, j) in a given array of unique strings `words` such that the concatenation of `words[i]` and `words[j]` forms a palindrome. The challenge is to find all such pairs with an efficient algorithm. The input is an array of strings, and the output is a list of lists, where each inner list contains two integers representing the indices of a palindrome pair. Constraints include the size of the `words` array and the length of each string within it.

Solution Approach

The provided solution uses a brute-force approach with optimizations. It iterates through each word in the input array `words`. For each word, it splits it into a prefix and a suffix at every possible position. Then, it checks two cases: first, if the suffix is a palindrome, it searches for the reversed prefix in the `words` array. Second, if the prefix is a palindrome, it searches for the reversed suffix in the `words` array. The use of a hash map allows efficient lookups during the reversed prefix and suffix searches. A set is used to avoid duplicate entries in the final result.

Step-by-Step Algorithm

  1. Step 1: Create a hash map (word_map) to store each word in `words` along with its index. This allows for O(1) lookup of word indices later.
  2. Step 2: Initialize an empty set `result` to store the palindrome pairs as tuples (i, j). Using a set avoids duplicate pairs.
  3. Step 3: Iterate through each word in the `words` array using index `i`.
  4. Step 4: For each word, iterate through all possible split positions `j` (from 0 to length of word).
  5. Step 5: Split the current word into `prefix` (words[i][0:j]) and `suffix` (words[i][j:])
  6. Step 6: Check if the `suffix` is a palindrome. If it is, reverse the `prefix` and check if the reversed `prefix` exists in `word_map` and is not the same word as `words[i]`. If both are true, add the pair (i, index of reversed prefix) to the `result` set.
  7. Step 7: Check if the `prefix` is a palindrome and `j != n` (to avoid double counting when the full word is a palindrome, which is already covered by j=0 in the first case). If it is, reverse the `suffix` and check if the reversed `suffix` exists in `word_map` and is not the same word as `words[i]`. If both are true, add the pair (index of reversed suffix, i) to the `result` set.
  8. Step 8: Convert the `result` set to a list of lists and return it.

Key Insights

  • Insight 1: A crucial observation is that if `words[i] + words[j]` is a palindrome, then we can potentially split `words[i]` into a prefix and a suffix, or `words[j]` into a prefix and suffix, and analyze whether one part is a palindrome and the other part's reverse exists in the `words` array.
  • Insight 2: Using a hash table (or map) to store each word and its index enables efficient lookups (O(1) on average) for the reversed strings.
  • Insight 3: It's important to consider cases where one of the strings is empty or when a string is itself a palindrome. Also, prevent adding duplicate pairs to the result.

Complexity Analysis

Time Complexity: O(n*k^2)

Space Complexity: O(n*k)

Topics

This problem involves: Array, Hash Table, String, Trie.

Companies

Asked at: Airbnb, Wix, Yandex.