Problem Description
Given an array of unique strings, find all pairs of indices (i, j) such that the concatenation of words[i] and words[j] forms a palindrome. The algorithm must work in O(sum of words[i].length) time.
Key Insights
- A palindrome is a string that reads the same forwards and backwards.
- For any given word, if you split it into two parts, one part (either prefix or suffix) might be a palindrome.
- If the other part’s reverse exists elsewhere in the array, then that reverse can be concatenated to yield a palindrome.
- Carefully handle edge cases like empty strings, since an empty string is a palindrome by default.
Space and Time Complexity
Time Complexity: O(n * k^2) in the worst case where n is the number of words and k is the maximum word length, but with the palindrome checking optimized this can be considered O(sum of word lengths): Each word is processed once and each split is checked in O(k) time. Space Complexity: O(n * k) due to the hashmap storing all words and their indices.
Solution
We use a hashmap (dictionary) to store each word and its corresponding index. For every word, we iterate through every possible split position to divide the word into a prefix and a suffix. For each split:
- If the prefix is a palindrome, we look for the reverse of the suffix in the hashmap. If found (and not the same word), then combining the reverse with the current word forms a palindrome.
- Similarly, if the suffix is a palindrome, we look for the reverse of the prefix.
We must be cautious with edge cases, especially when either part is empty, as the empty string is considered a palindrome. This method avoids checking every possible pair explicitly and leverages string reversal and palindrome checks to achieve efficiency.