336. 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"]

分析:http://bookshadow.com/weblog/2016/03/10/leetcode-palindrome-pairs/

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

遍历单词列表words,记当前单词为word,下标为idx:

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

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

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

     3.1) 若left为回文,并且right的逆序串在words中,则将right的逆序串下标rridx与idx加入答案
     
     3.2) 若right为回文,并且left的逆序串在words中,则将left的逆序串下标idx与rlidx加入答案
 1 public class Solution {
 2     public List<List<Integer>> palindromePairs(String[] words) {
 3         final String EMPTY_STRING = ""; 
 4         List<List<Integer>> result = new ArrayList<>();
 5         HashMap<String, Integer> map = new HashMap<>();
 6         for (int i = 0; i < words.length; i++) {
 7             map.put(words[i], i);
 8         }
 9 
10         for (int i = 0; i < words.length; i++) {
11             String s = words[i];
12 
13             // if the word is a palindrome, get index of ""
14             if (isPalindrome(s)) {
15                 if (map.containsKey(EMPTY_STRING) && map.get(EMPTY_STRING) != i) {
16                     result.add(indexList(i, map.get(EMPTY_STRING)));
17                     result.add(indexList(map.get(EMPTY_STRING), i));
18                 }
19             }
20 
21             // if the reversed word exists, it is a palindrome, e.g., bat tab
22             String reversed = new StringBuilder(s).reverse().toString();
23             if (map.containsKey(reversed) && map.get(reversed) != i) {
24                 result.add(indexList(i, map.get(reversed)));
25             }
26 
27             for (int k = 1; k < s.length(); k++) {
28                 String left = s.substring(0, k);
29                 String right = s.substring(k);
30 
31                 // if left part is palindrome, find reversed right part
32                 if (isPalindrome(left)) {
33                     String reversedRight = new StringBuilder(right).reverse().toString();
34                     if (map.containsKey(reversedRight)) {
35                         result.add(indexList(map.get(reversedRight), i));
36                     }
37                 }
38 
39                 // if right part is a palindrome, find reversed left part
40                 if (isPalindrome(right)) {
41                     String reversedLeft = new StringBuilder(left).reverse().toString();
42                     if (map.containsKey(reversedLeft)) {
43                         result.add(indexList(i, map.get(reversedLeft)));                        
44                     }
45                 }
46             }
47         }
48         return result;
49     }
50 
51     public boolean isPalindrome(String s) {
52         for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
53             if (s.charAt(i) != s.charAt(j)) {
54                 return false;
55             }
56         }
57         return true;
58     }
59     
60     public List<Integer> indexList(int idx1, int idx2) {
61         return Arrays.asList(new Integer[] {idx1, idx2});
62     }
63 }

 

posted @ 2016-08-01 00:32  北叶青藤  阅读(342)  评论(0编辑  收藏  举报