Watch 10 video solutions for Palindrome Pairs, a hard level problem involving Array, Hash Table, String. This walkthrough by Coding Decoded has 13,931 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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, andwords[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 <= 50000 <= words[i].length <= 300words[i] consists of lowercase English letters.Problem Overview: You are given a list of unique words. The task is to return all pairs of indices (i, j) such that concatenating words[i] + words[j] forms a palindrome. The challenge is efficiently checking many string combinations without recomputing palindrome checks repeatedly.
Approach 1: Brute Force Check with Reversed Strings (O(n2 * k) time, O(1) extra space)
The straightforward solution checks every ordered pair of words. For each pair (i, j), concatenate the two strings and verify if the result is a palindrome using two pointers from both ends. With n words and average length k, each concatenation check costs O(k), giving overall complexity O(n^2 * k). A small improvement is to precompute reversed versions of each string so you can quickly compare segments instead of building full concatenations. This approach relies mainly on simple array iteration and string comparison. It works well for small input sizes but quickly becomes slow when the number of words grows.
Approach 2: Using Trie Data Structure (O(n * k^2) time, O(n * k) space)
A more scalable approach builds a Trie containing reversed words. While inserting each reversed word, store indices of words whose remaining prefix forms a palindrome. This extra bookkeeping allows efficient matching later. When processing a word, iterate through its characters and walk the Trie simultaneously. If you encounter a Trie node that marks the end of another word and the remaining substring of the current word is a palindrome, you found a valid pair.
After reaching the end of the word in the Trie, any stored indices in that node represent words whose leftover suffix is already palindromic. Those indices also form valid pairs. This technique avoids comparing every pair directly and instead performs structured lookups. The Trie organizes reversed words by prefix, which makes matching efficient. The method combines concepts from Trie, hash table-style indexing, and repeated palindrome checks on substrings.
The key insight is splitting each word at every possible boundary. If the prefix is a palindrome, the reversed suffix may match another word. If the suffix is a palindrome, the reversed prefix may match. The Trie helps locate those matches quickly without scanning all words.
Recommended for interviews: Start with the brute force approach to show understanding of the problem and correctness of palindrome checks. Interviewers typically expect a more optimized design afterward. The Trie-based solution demonstrates stronger algorithmic thinking and knowledge of advanced string indexing techniques, which is why it is the preferred discussion in most senior-level interviews.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Checking | O(n^2 * k) | O(1) | Good for understanding the problem or when the number of words is small |
| Trie with Reversed Words | O(n * k^2) | O(n * k) | Best for large inputs where checking all pairs is too slow |