原题链接在这里: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 = ["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"]

题解:

可以合成Palindrome Pairs有几种情况:

1. ["abc", "cba"]

2. ["aabc", "cb"]

3. ["cbaa", "bc"]

要么有个当前string的reverse过来的string也存在,要么当前string的左半部分或者右半部分已经是palindrome, 剩下部分有reverse过来的string存在.

先用HashMap把原有string 和对应index保存。然后对于每一个string拆成left 和 right两个substring, 若是其中一个substring本身就是palindrom, 就看另一个substring的reverse是不是存在.

当然""也是palindrome, 所以如果左右有""存在, 那就是看left, right本身有没有对应的reverse存在.

Note: 要注意["abc", "cba"], 一个substring为""的情况只检查一遍. 不然先检查"abc", left = "", right = "abc", 或者right = "", left = "abc", reverse都存在,就会加[0,1], [1,0]. 等再检查 "cba"时 又会重复加一遍结果. 所以第二个check时要加上right.length() != 0.

注意 i != hm.get(reverseR), 不然会加上[3, 3]. 自己与自己配对的情况.

Time Complexity: O(n * len * len), n = words.length, len时word的平均长度.

Space: O(n), regardless res.

AC Java:

 public class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(words == null || words.length < 2){
return res;
} HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i = 0; i<words.length; i++){
hm.put(words[i], i);
} for(int i = 0; i<words.length; i++){
for(int j = 0; j<=words[i].length(); j++){ //j是能到word[i].length()的
String left = words[i].substring(0, j);
String right = words[i].substring(j); if(isPalindrome(left)){
String reverseRight = new StringBuilder(right).reverse().toString();
if(hm.containsKey(reverseRight) && hm.get(reverseRight)!=i){
List<Integer> item = new ArrayList<Integer>();
item.add(hm.get(reverseRight));
item.add(i);
res.add(item);
}
}
if(isPalindrome(right)){
String reverseLeft = new StringBuilder(left).reverse().toString();
if(hm.containsKey(reverseLeft) && hm.get(reverseLeft) != i && right.length()!=0){
//Addition check is right.length() != 0
//Or will add duplicate results, like ["abc", "cba"]
List<Integer> item = new ArrayList<Integer>();
item.add(i);
item.add(hm.get(reverseLeft));
res.add(item);
}
}
}
}
return res;
} private boolean isPalindrome(String s){
int l = 0;
int r = s.length()-1;
while(l<=r){
if(s.charAt(l++) != s.charAt(r--)){
return false;
}
}
return true;
}
}

类似Longest Palindromic SubstringShortest Palindrome.

LeetCode 336. Palindrome Pairs的更多相关文章

  1. leetcode@ [336] Palindrome Pairs (HashMap)

    https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...

  2. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  3. 336. Palindrome Pairs(can't understand)

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  4. leetcode 132 Palindrome Pairs 2

    lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...

  5. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

  6. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  7. 【leetcode】336. Palindrome Pairs

    题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...

  8. [Leetcode] 336. Palindrome Pairs_Hard

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  9. 336 Palindrome Pairs 回文对

    给定一组独特的单词, 找出在给定列表中不同 的索引对(i, j),使得关联的两个单词,例如:words[i] + words[j]形成回文.示例 1:给定 words = ["bat&quo ...

随机推荐

  1. c# 单例模式(Single);单例模式的5种写法

    单例模式(Singleton Pattern): 在平时的开发中,可能会用到单例模式,许多java的笔试题中也会叫笔试者写出单例模式的那几种写法并且分析.那么下面就来轻轻地探讨一下,最简单的设计模式, ...

  2. [转] 我所经历的IBM SOA与系统整合

    2006年,一汽大众新上来一位总经理,新加坡人,整个一国际背景高端管理人才,是德国人和中国人博弈后取得的胜利. 一汽大众过去是从总部到区域到终端一体化的大盘管理模式,很僵化很粘稠,不利用快速反应.于是 ...

  3. java的服务端与客户端通信(2)

    一.Socket连接与HTTP连接   1.1Socket套接字 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信 ...

  4. mysql ERROR 1045 (28000): Access denied (using password: YES)

    mysql 安装完成后 mysql -u root -p #让输入密码直接回车就能登录 设置mysql的root用户初始密码: mysqladmin -u root password 'root' 解 ...

  5. sql中in和exists的区别效率问题 转

    in 和exists in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果 ...

  6. pppoe白皮书

    转:https://blog.csdn.net/achejq/article/details/19478811 PPPoE技术白皮书 关键词:PPP,Ethernet,PPPoE 摘    要:PPP ...

  7. 20145210姚思羽 《网络对抗技术》 Web安全基础实践

    20145210姚思羽 <网络对抗技术> Web安全基础实践 实验后回答问题 1.SQL注入攻击原理,如何防御 ·SQL攻击的原理很简单,就是在用户名输入框里输入SQL语句,来欺骗数据库服 ...

  8. 20165101刘天野 2017-2018-2 《Java程序设计》第7周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章JDBC与MySQL数据库 JDBC简介 JDBC(Java Databas ...

  9. mongodb 中 Aggregation 的管道和分片集合( Pipeline and Sharded Collections)

    mongodb 中的aggretion 中,如果管道中存在一个与之相匹配的shard key ,那么这个管道只运行在与之相匹配的shard 中,在以前(3.2),pipeline 被分流,最后又由pr ...

  10. 完全重装python和yum

    本文原链接 http://smilepad.blog.51cto.com/6094369/1333478 http://blog.etc168.com/?p=642 1.删除现有Python #roo ...