Palindrome Pairs 解答
Question
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"]
Answer
Brute-force的做法是两层for循环,遍历每个string,看有无string可以与之匹配为palindrome。
较好的做法是利用HashMap。
对于String a来说,考虑两种情况:
1. a为空,则a与集合中任意已经对称的string可以组成结果
2. a不为空。
则有三种情况:
a. reverse(a)在集合中
b. a[0..i]对称,且reverse(a[i + 1,...,n])在集合中。如"aaabc", "cba"
c. a[i,..,n]对称,且reverse(a[0,...,i])在集合中。如"abcc", "ba"
public class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> result = new ArrayList<>();
if (words == null || words.length == 0) {
return result;
}
// Record each string position
Map<String, Integer> map = new HashMap<>();
Set<Integer> palindromeSet = new HashSet<>();
int len = words.length;
for (int i = 0; i < len; i++) {
map.put(words[i], i);
if (isPalindrome(words[i])) {
palindromeSet.add(i);
}
}
// Traverse
for (int i = 0; i < len; i++) {
String word = words[i];
if (word.length() == 0) {
for (int index : palindromeSet) {
addResult(result, i, index);
}
}
int l = word.length();
for (int j = 0; j < l; j++) {
String front = word.substring(0, j);
String back = word.substring(j, l);
String rFront = reverse(front);
String rBack = reverse(back);
if (isPalindrome(front) && map.containsKey(rBack)) {
addResult(result, map.get(rBack), i);
}
if (isPalindrome(back) && map.containsKey(rFront)) {
addResult(result, i, map.get(rFront));
}
}
}
return result;
} private String reverse(String s) {
StringBuilder sb = new StringBuilder(s);
return sb.reverse().toString();
} private void addResult(List<List<Integer>> result, int start, int end) {
if (start == end) {
return;
}
List<Integer> indexes = new ArrayList<>();
indexes.add(start);
indexes.add(end);
result.add(indexes);
} private boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
}
Palindrome Pairs 解答的更多相关文章
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- LeetCode 336. Palindrome Pairs
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- 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 ...
- 【题解】Palindrome pairs [Codeforces159D]
[题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...
- 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】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
随机推荐
- ICMP报文分析
一.概述: 1. ICMP同意主机或路由报告差错情况和提供有关异常情况.ICMP是因特网的标准协议,但ICMP不是高层协议,而是IP层的协议.通常ICMP报文被IP层或更高层协议(TCP或UDP) ...
- 关于js跨域
get方式: 称为jsonp,就是js的跨域通信方式,因为知道有些标签可以跨域获取内容,例如img,script,link...,jsonp就是把动态创建一个script标签,然后配置src属性,后台 ...
- SharedPreferences的工具类,使用起来方便、快捷
SharedPreferences的工具类,使用起来方便.快捷:上代码:import android.content.Context;import android.content.SharedPref ...
- AJAX校验用户名是否存在,焦点离开用户名、点击 【 检 查用户名 】的校验。分别用 XMLHttp 和 JQueryAJAX实现。
XMLHttp方法: $("#name").blur(function () { var xmlhttp = new ActiveXObject("Microsoft. ...
- Andoid源码 BUG修改集合--不断更新
BUG001:很抱歉,***已停止运行 网上查找问题原因很多,有人说事缓存不够,作为一个开发者,需要从代码解决问题 比如,这次遇到一个"很抱歉,instant已停止运行",inst ...
- Activity的学习
安卓的四大组件分别是 Activity ,Service服务, BroadcastReceiver广播接收器,ContentProvide内容提供器 . Activity: Activity是应用程序 ...
- github修改自己的昵称
由于刚接触github,不会用,就随便写了个昵称,后来想改,却不知道从哪里改,到百度搜结果都是说不能修改的(这里就不吐槽百度了),还是直接上图吧. 点击Settings,然后跳转到下面界面,点击Acc ...
- JavaWeb 之 重复提交表单和验证码相关的问题!
下面我们首先来说一下表单的重复提交问题,我们知道在真实的网络环境中可能受网速带宽的原因会造成页面中表单在提交的过程中出现网络的延迟等问题,从而造成多次提交的问题!下面我们就具体来分析一下造成表单提交的 ...
- KVM virt-manager使用.
本来不想写,但是觉得教程就应该详细点..所以又有了这篇文章..主要是对图形化kvm管理的一些说明 接着上一篇... 1.Virtual Machine Manager 摘要: 打开Virtual Ma ...
- 2.2 文件 I/O 的基石:Path
Path通常代表文件系统中的位置,能浏览任何类型的文件系统,包括zip归档文件系统: 文件系统中的几个概念:目录树.根目录.绝对路径.相对路径: NIO.2中的Path是一个抽象构造,你所创建和处理的 ...