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         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 }
时间: 2024-12-21 22:58:01

Palindrome Pairs的相关文章

[LeetCode]Palindrome Pairs

题目: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",

DP VK Cup 2012 Qualification Round D. Palindrome pairs

题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 1 /* 2 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 3 DP:很巧妙的从i出发向两头扩展判断是否相同来找回文串 4 dpr[i] 代表记录从0到i间的回文子串的个数,dpl[i] 代表记录i之后的回文子串个数 5 两两相乘就行了 6 详细解释:http://blog.csdn.net/shiyuankongbu/article/details

codeforces159D - Palindrome pairs 二重DP

题意:给你一个字符串,问你其中不重叠的回文字串对有多少 解题思路:这题用到两种方法,不过其实是一个很巧妙的二重dp 1)暴力求解以i开头,j结尾的是否为回文,如果是,ans += sum[i-1](ans 为答案, sum[i]为在  1 - i内回文串的个数--需要dp求解) 这里非常耗时,时间大约为  n^3 ,  跑出来为 830ms 解题代码: 1 // File Name: 159d.cpp 2 // Author: darkdream 3 // Created Time: 2014年

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"]Retu

[LeetCode] 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"]Ret

【Leetcode】Palindrome Pairs

题目链接:https://leetcode.com/problems/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 

leetcode 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"

【LeetCode】Palindrome Pairs(336)

1. Description 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", &qu

[email&#160;protected] [336] Palindrome Pairs (HashMap)

https://leetcode.com/problems/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 =