Talentd
FleetCode

336. Palindrome Pairs

Hard37.7% acceptance8 companies

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 ["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 <= 5000
  • 0 <= words[i].length <= 300
  • words[i] consists of lowercase English letters.

Hints open one at a time

Hint 1
Checking every two pairs will exceed the time limit. It will be O(n^2 * k). We need a faster way.
Hint 2
If we hash every string in the array, how can we check if two pairs form a palindrome after the concatenation?
Hint 3
We can check every string in words and consider it as words[j] (i.e., the suffix of the target palindrome). We can check if there is a hash of string that can be the prefix to make it a palindrome.
10:47
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def palindromePairs(self, words: List[str]) -> List[List[int]]:
3
Starter codePython 3Spaces: 4
Examples
Input
["abcd","dcba","lls","s","sssll"]
Input
["bat","tab","cat"]
Input
["a",""]
Your code is saved on this device.Solution