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 ...
随机推荐
- 加入gitignore文件没有起作用怎么办
步骤一: 假设有未提交的文件先提交到Git. 步骤二: 在Git根文件夹下运行以下的Git命令: git rm -r --cached . git add . git commit -m " ...
- Android开发之扫描附近wifi热点并列表显示
近期项目中用到了wifi模块.今天做一个简单的总结. 參考:http://www.2cto.com/kf/201310/253617.html 1.如何获取wifi对象并进行操作 要操作WIFI设备, ...
- COM编程入门第二部分——深入COM服务器
本文为刚刚接触COM的程序员提供编程指南,解释COM服务器内幕以及如何用C++编写自己的接口.继上一篇COM编程入门之后,本文将讨论有关 COM服务器的内容,解释编写自己的COM接口和COM服务器所需 ...
- mysql中如何更新一个字段的值为它本身的值连接上一个字符串
CONCAT(str1,str2,...) 返回结果为连接参数产生的字符串. 如有任何一个参数为NULL ,则返回值为 NULL. 或许有一个或多个参数. 如果所有参数均为非二进制字符串,则结 ...
- 数据结构笔记01:编程面试过程中常见的10大算法(java)
以下是在编程面试中排名前10的算法相关的概念,我会通过一些简单的例子来阐述这些概念.由于完全掌握这些概念需要更多的努力,因此这份列表只是作为一个介绍.本文将从Java的角度看问题,包含下面的这些概念: ...
- 10、第十节课jq420151012
1.点击交替显示隐藏功能 点击交替执行的:fadeToggle(1000) , slideToggle() , toggle(1000); 2.点击单独执行 单独显示/隐藏:sho ...
- nyoj 42
#include <iostream> #include <stdio.h> #include <cstring> #include <algorithm&g ...
- Jquery+css实现图片无缝滚动轮播
Today,在XX学院的教学视频中,偶尔看到了Jquery+css实现图片无缝滚动轮播视频教程,虽然以前已写过类似的,但是我感觉他学的比较精简.为了方便以后做项目时直接拷贝,特地写出来,顺便和大家分享 ...
- 《CSS网站布局实录》学习笔记(六)
第六章 CSS高级应用与技巧 6.1 id与class 6.1.1 什么是id id是XHTML元素的一个属性,用于标识对象名称.无论是class还是id,都是XHTML所支持的公共属性,并且也是其核 ...
- Objective-C学习篇07—NSArray与NSMutableArray
大纲 NSArray NSMutableArray 快速枚举 NSArray NSArray是一个静态数组,也就是一个不可变数组,一旦创建以后,就不能进行添加,删除或者修改其中的元素.NSArray继 ...