原题链接在这里: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. PAT 天梯赛 L1-039. 古风排版 【字符串处理】

    题目链接 https://www.patest.cn/contests/gplt/L1-039 思路 先根据 len 和 n 判断 有几个 列和几行,然后 从最右边 到 最左边 从上到下 将字符串 录 ...

  2. $git学习总结系列(1)——基本用法

    廖雪峰的官方网站:http://www.liaoxuefeng.com/ 本文是学习廖雪峰的官方网站上git教程git基本用法的总结,详细内容可以进入廖雪峰的官方网站查看. 注:本文中的主要内容都是基 ...

  3. java经典30笔试题

    1. 下面哪些是Thread类的方法() A start()       B run()       C exit()       D getPriority() 答案:ABD 解析:看Java AP ...

  4. CSS3手风琴下拉菜单

    在线演示 本地下载

  5. 20145230《java程序设计》第6周学习总结

    20145230 <Java程序设计>第6周学习总结 教材学习内容 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取出,可以 ...

  6. iOS_Quartz 2D绘图

    目  录: 一.基础知识掌握 二.Quartz 2D绘图基础:CGContextRef实现简单地绘制图形 三.CGContextRef实现文字.图片.基于路径的图形绘制 四.在内存中绘制位图 五.添加 ...

  7. Qt Ping

    QProcess对象可以直接执行cmd的命令,但是ping操作是会阻塞的,所以需要在子线程里ping QProcess *tempCmd = new QProcess(); tempCmd->s ...

  8. INSPIRED启示录 读书笔记 - 第28章 创业型公司的产品管理

    产品设计方式 第一步:创业初期只设三个职位,产品经理.交互设计师和原型开发人员(职位可以兼任) 第二步:快速展开产品设计(高保真原型),邀请真实的目标用户验证产品原型,迭代修改 第三步:随着迭代的深入 ...

  9. 自动生成Mapper代码

    public class BeanMapperTest { @Test public void build() throws Exception { Class clazz = RiskAccess. ...

  10. https阿里云证书购买与apache环境配置

    1.在阿里云云盾安全->CA证书购买 2.下载证书解压文件,一般有四个文件 3.在/etc/apache2  下创建一个文件夹cert 放入以上四个文件(路径可自己任意选择) 4.$sudo a ...