Given a list of unique words, find all pairs of distinct indices [i,j] in the given list, so that the concatenation of the two words, i.e. words[i] + word[j] is a palindrome.
Example 1:
Input: ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]
Example 2:
Input: ["bat","tab","cat"]
Output: [[0,1],[1,0]]
Explanation: The palindromes are ["battab","tabbat"]
Time Complexity: O(nk^2). 我们需要用O(n)的时间遍历所有的字符串,然后对于每个字符串(平均长度假设为k),我们需要将其拆分k次,且对每一个此拆分,我们都需要用再用O(k)的时间检查其是否为panlindrome、翻转不是panlindrome的部分。因此,总时间复杂度为O(n k^2)
Space Complexity: O(n). 我们需要使用额外的HashMap储存所有的String。如果我们考虑每次中间拆分过程中创造的String所占用空间,则复杂度为O(nk).