码迷,mamicode.com
首页 > 其他好文 > 详细

Palindrome Pairs

时间:2016-08-01 01:44:48      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:

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         List<List<Integer>> result = new ArrayList<List<Integer>>();
 4 
 5         HashMap<String, Integer> map = new HashMap<String, Integer>();
 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("") && map.get("") != i) {
16                     ArrayList<Integer> l = new ArrayList<Integer>();
17                     l.add(i);
18                     l.add(map.get(""));
19                     result.add(l);
20 
21                     l = new ArrayList<Integer>();
22 
23                     l.add(map.get(""));
24                     l.add(i);
25                     result.add(l);
26                 }
27             }
28 
29             // if the reversed word exists, it is a palindrome, e.g., bat tab
30             String reversed = new StringBuilder(s).reverse().toString();
31             if (map.containsKey(reversed) && map.get(reversed) != i) {
32                 ArrayList<Integer> l = new ArrayList<Integer>();
33                 l.add(i);
34                 l.add(map.get(reversed));
35                 result.add(l);
36             }
37 
38             for (int k = 1; k < s.length(); k++) {
39                 String left = s.substring(0, k);
40                 String right = s.substring(k);
41 
42                 // if left part is palindrome, find reversed right part
43                 if (isPalindrome(left)) {
44                     String reversedRight = new StringBuilder(right).reverse().toString();
45                     if (map.containsKey(reversedRight)) {
46                         ArrayList<Integer> l = new ArrayList<Integer>();
47                         l.add(map.get(reversedRight));
48                         l.add(i);
49                         result.add(l);
50                     }
51                 }
52 
53                 // if right part is a palindrome, find reversed left part
54                 if (isPalindrome(right)) {
55                     String reversedLeft = new StringBuilder(left).reverse().toString();
56                     if (map.containsKey(reversedLeft)) {
57                         ArrayList<Integer> l = new ArrayList<Integer>();
58                         l.add(i);
59                         l.add(map.get(reversedLeft));
60                         result.add(l);                        
61                     }
62                 }
63             }
64         }
65         return result;
66     }
67 
68     public boolean isPalindrome(String s) {
69 
70         int i = 0;
71         int j = s.length() - 1;
72 
73         while (i < j) {
74             if (s.charAt(i) != s.charAt(j)) {
75                 return false;
76             }
77             i++;
78             j--;
79         }
80         return true;
81     }
82 }

 

Palindrome Pairs

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5724408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!