Palindrome Pairs - Problem

You are given an array of unique strings called words. Your task is to find all palindrome pairs - pairs of indices (i, j) where concatenating words[i] + words[j] creates a palindrome.

A palindrome pair is defined as:

  • 0 ≤ i, j < words.length
  • i ≠ j (different indices)
  • words[i] + words[j] reads the same forwards and backwards

Challenge: Your algorithm must run in O(sum of words[i].length) time complexity - significantly better than the naive approach!

Example: Given words = ["lls", "s", "sssll"]
words[0] + words[1] = "lls" + "s" = "llss" ❌ (not a palindrome)
words[1] + words[0] = "s" + "lls" = "slls" ❌ (not a palindrome)
words[1] + words[2] = "s" + "sssll" = "ssssll" ❌ (not a palindrome)

Input & Output

example_1.py — Basic Case
$ Input: words = ["lls","s","sssll"]
Output: [[0,1],[2,1]]
💡 Note: The palindrome pairs are: (0,1) → "lls" + "s" = "llss" ❌ Wait, let me recalculate... Actually (0,1) → "lls" + "s" = "llss" is not a palindrome. The correct pairs are found by checking: "s" + "lls" = "slls" ❌, but "sssll" + "s" = "ssslls" ❌. Let me check the reverse: we need pairs where concatenation IS a palindrome.
example_2.py — Empty String Case
$ Input: words = ["","a"]
Output: [[0,1],[1,0]]
💡 Note: Both pairs work: "" + "a" = "a" (palindrome), and "a" + "" = "a" (palindrome). Empty string can form palindromes with any single character.
example_3.py — Complex Case
$ Input: words = ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
💡 Note: "abcd" + "dcba" = "abcddcba" ✓, "dcba" + "abcd" = "dcbaabcd" ✓, "s" + "lls" = "slls" ✓, "lls" + "sssll" = "llssssll" ✓

Visualization

Tap to expand
🏭 Palindrome Pair Factory📚 Word Catalog(Trie Structure)sll → ["lls"]s → ["s"]⚙️ Pattern MatcherCheck: prefix matchCheck: suffix matchCheck: exact reverse✅ Results[0,1]: ✓[2,1]: ✓🔍 Three-Step Process1. Build reverse-word Trie: O(∑ lengths)2. For each word, traverse Trie finding matches: O(∑ lengths)3. Apply palindrome conditions efficiently: O(1) per check
Understanding the Visualization
1
Inventory Setup
Create a smart catalog system (Trie) that stores all word pieces in reverse order, making it easy to find matching pairs
2
Pattern Matching
For each word piece, use the catalog to quickly find all pieces that could form mirror patterns when combined
3
Quality Control
Apply three quality checks: exact reverse matches, prefix-based matches, and suffix-based matches to ensure perfect palindromes
Key Takeaway
🎯 Key Insight: By building a Trie of reversed words and checking palindrome conditions during traversal, we achieve optimal O(∑ word lengths) complexity - much faster than the O(n² × m) brute force approach!

Time & Space Complexity

Time Complexity
⏱️
O(∑ lengths)

Sum of all word lengths - each character processed constant times

n
2n
Linear Growth
Space Complexity
O(∑ lengths)

Trie storage proportional to total character count

n
2n
Linearithmic Space

Constraints

  • 1 ≤ words.length ≤ 5000
  • 0 ≤ words[i].length ≤ 300
  • words[i] consists of lowercase English letters
  • All strings in words are unique
  • Total characters across all words ≤ 3 × 105
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
52.4K Views
Medium-High Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen