Palindrome Pairs

题目描述:

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] + words[j] is a palindrome.

Example 1:
Given words = ["bat", "tab", "cat"]
Return [[0, 1], [1, 0]]
The palindromes are ["battab", "tabbat"]

Example 2:
Given words = ["abcd", "dcba", "lls", "s", "sssll"]
Return [[0, 1], [1, 0], [3, 2], [2, 4]]
The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"]


暴力解法超时。换个思路:

利用字典map保存单词 -> 下标的键值对

遍历单词列表

1). 若当前单词word本身为回文,且words中存在空串,则将空串下标与index加入答案

2). 若当前单词的逆序串在words中,则将逆序串下标与index加入答案

3). 将当前单词word拆分为左右两半left,right。

     3.1) 若left为回文,并且right的逆序串在words中,则将right的逆序串下标与idx加入答案
     
     3.2) 若right为回文,并且left的逆序串在words中,则将left的逆序串下标与idx与加入答案

代码如下:



你可能感兴趣的:(java,LeetCode,HashMap,回文串)