Palindromes Pairs
Solve a hard-level problem on finding palindrome pairs using tries.
We'll cover the following...
Problem statement
Given an array words of unique strings, return an array of index pairs
Example 1
Sample input
words: ["efgh","aap","p","pppaa","hgfe"]
Sample output
[[0,4],[4,0],[2,1],[1,3]]
Explanation
The palindromes are:"efghhgfe" = words[0] + words[4] = "efgh" + "hgfe""hgfeefgh" = words[4] + words[0] = "hgfe" + "efgh""paap" = words[2] + words[1] = "p" + "aap""aappppaa" = words[1] + words[2] = "aap" + "pppaa"
Example 2
Sample input
words: ["no","on","at"]
Sample output
[[0,1],[1,0]]
Explanation
The palindromes are:"noon" = words[0] + words[1] = "no" + "on""onno" = words[4] + words[0] = "on" + "no"
Example 3
Sample input
words: ["fff",""]
Sample output
[[0,1],[1,0]]
Explanation
The palindromes are:"fff" = words[0] + words[1] = "fff" + """fff" = words[1] + words[0] = "" + "fff"
Try it yourself
Try to solve the problem yourself before reading the solution.
Intuition
To solve this problem, let's first explore the properties of string pairs that form a palindrome.
Let's say that a palindrome is formed from three strings
String
For example: