原题链接在这里: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. iOS Xcode 8 快捷键 (注释 失效 处理)

    在升级后,好用的VVDocumment 插件不能用了.(但是苹果这次内置了好多好用的插件,也有自己的注释功能了 AddDocumentation) 上网上有查到 传播很广泛的一条信息 "这个 ...

  2. iOS NSCoding 的学习 和 使用

    起初接触的轻量级 物理存储 方式 是 plist  可以存储 系统级别的 字典 数组   但是不能存储自定义的对象类 那会 用自定义对象做存储的 需求也不大 主要 是 还没建立面向对象意识,会的也少. ...

  3. 014_HDFS存储架构、架构可靠性分析、副本放置策略、各组件之间的关系

    1.HDFS存储架构

  4. Vuex的入门教程

    前言 在 Vue.js 的项目中,如果项目结构简单, 父子组件之间的数据传递可以使用  props 或者 $emit 等方式,详细点击这篇文章查看. 但是如果是大型项目,很多时候都需要在子组件之间传递 ...

  5. Linux centos7 防火墙设置

    1.查看防火墙状态 systemctl list-unit-files|grep firewalld.service 或 systemctl status firewalld.service 2.开启 ...

  6. 活用:after 让图片垂直居中

    现在莫名虽然更喜欢 background 但大多时候还是选择用 img,这其中的利弊争议不在本文中赘述. 那么在布局中常会遇到定高容器中图片居中的需求,这时就有很多方法了呀: line-height ...

  7. PHP面向对象之对象和引用

    在PHP中对象类型和简单变量类型表现可以说是大相径庭,很多数据类型都要可以在写时进行复制,如当写代码$a=$b时,两个变量因为赋予相同的值而告终.所以需要注意的是,这种情况用在对象时就会完全不同了. ...

  8. JavaWeb Session

    1. Session概述 1.1. 什么是Session Session一般译为会话,是解决Http协议的无状态问题的方案,可以将一次会话中的数据存储在服务器端的内存中,保证在下一次的会话中可以使用. ...

  9. RDLC 微软报表 自定义函数

    报表的空白处点右键,报表属性,CODE,按下面的格式输入自定义函数: Shared Function ShowDate(value as DateTime) as string if value< ...

  10. eclipse中web项目部署到本地tomcat中,但是在本地的tomcat的webapp下找不到发布的项目

    eclipse不像MyEclipse默认将项目部署到tomcat安装目录下的webapps中,而默认部署到工作目录下 为了使项目默认部署到tomcat安装目录下的webapps中,show view- ...