LeetCode 336. 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 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 Substring, Shortest Palindrome.
LeetCode 336. Palindrome Pairs的更多相关文章
- leetcode@ [336] Palindrome Pairs (HashMap)
https://leetcode.com/problems/palindrome-pairs/ Given a list of unique words. Find all pairs of dist ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 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 ...
- leetcode 132 Palindrome Pairs 2
lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...
- leetcode 131 Palindrome Pairs
lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 【leetcode】336. Palindrome Pairs
题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...
- [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 ...
- 336 Palindrome Pairs 回文对
给定一组独特的单词, 找出在给定列表中不同 的索引对(i, j),使得关联的两个单词,例如:words[i] + words[j]形成回文.示例 1:给定 words = ["bat&quo ...
随机推荐
- Python 3 文件和字符编码
一.文件: 打开文件的模式有: r,只读模式(默认). w,只写模式. 不可读,不存在则创建:存在则删除内容 a,追加模式. 可读,不存在则创建:存在则只追加内容 "+"表示可以 ...
- 小数据池、is 和 ==的区别
小数据池,在一定情况下出现内存共享(只有int 和 str 才有的) is 和 ==的区别 id() 打印数据的地址 a = 'hello' b = 'hello' print(a = ...
- PHP实现生成唯一编号(36进制的不重复编号)
当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号.10位的某证件号码.订单流水号.短网址等等,我们可以使用36进制计算出符合位数的不重复的编号. 我们将0-Z(012345678 ...
- 美图秀秀 web开发图片编辑器
美图秀秀web开发平台 http://open.web.meitu.com/wiki/ 1.环境配置 1.1.设置crossdomain.xml 下载crossdomain.xml文件,把解压出来的c ...
- linux下查找文件或目录(which,whereis,locate,find)
命令 查找对象 查找速度 备注 which 可执行文件 根据PATH变量的内容去寻找可执行文件 不同的PATH配置会有不一样的结果 whereis 程序名的搜索 根据数据库来寻找,速度快 ...
- Linux下的文件查找命令——find
Linux下几个常见的文件查找命令: which 查看可执行文件的位置 whereis 寻找特定文件,查看文件的位置 locate 配合数据库查看文件位置 find ...
- Java Map增删改查
示例代码: 学生类 package com.imooc.collection; import java.util.HashSet; import java.util.Set; /** * 学生类 * ...
- Struts2全局异常处理
1.在struts.xml中配置全局异常处理 在Action中抛出异常,此异常可以是action自己抛的,也可以是Service抛出来的,都会跳转到全局异常中,只有在当前Action中配置域全局异常返 ...
- SpringBoot2.0之整合Kafka
maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...
- jsp路径问题
绝对路径:/StudentInfo/images/login.jpg 相对路径:images/login.jpg 路径前面的第一个/代表tomcate目录下面的webapps这个文件夹 jsp的Adv ...