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.lengthi ≠ 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
Visualization
Time & Space Complexity
Sum of all word lengths - each character processed constant times
Trie storage proportional to total character count
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