leetcode@ [336] Palindrome Pairs (HashMap)
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"]
public class Solution {
public static boolean isPalindrome(StringBuffer sb) {
if(sb == null || sb.length() == 0) return true;
int l = 0, r = sb.length()-1;
while(l < r) {
if(sb.charAt(l) != sb.charAt(r)) return false;
++l; --r;
}
return true;
}
public static void buildMappings(String[] words, HashMap<String, HashSet<String> > mapping) {
for(int i=0; i<words.length; ++i) {
StringBuffer sb = new StringBuffer(words[i]);
HashSet<String> adj = new HashSet<String> ();
for(int j=0; j<=sb.length(); ++j) {
/** partition words[i] into two parts */
StringBuffer prefix = new StringBuffer(sb.substring(0, j));
StringBuffer suffix = new StringBuffer(sb.substring(j));
/** check whether or not the prefix of words[i] is palindrome */
/** if it does, then the prefix can be the rotated point, otherwise not*/
if(isPalindrome(prefix)) {
/** adj stores the strings where suffix + words[i] is palindrome */
adj.add(suffix.reverse().toString());
}
if(isPalindrome(suffix)) {
/** adj stores the strings where words[i] + prefix is palindrome */
adj.add(prefix.reverse().toString());
}
}
/** add it to mapping */
mapping.put(sb.toString(), adj);
}
HashSet<String> hs = new HashSet<String> ();
for(int i=0; i<words.length; ++i) {
StringBuffer sb = new StringBuffer(words[i]);
if(isPalindrome(sb)) {
hs.add(sb.toString());
}
}
for(int i=0; i<words.length; ++i) {
if(words[i].equals("")) {
HashSet<String> adj = new HashSet<String> ();
adj.addAll(hs);
mapping.put(words[i], adj);
}
}
}
public List<List<Integer>> palindromePairs(String[] words) {
HashSet<List<Integer> > hres = new HashSet<List<Integer> > ();
List<List<Integer> > res = new ArrayList<List<Integer> > ();
HashMap<String, HashSet<String> > mapping = new HashMap<String, HashSet<String> > ();
buildMappings(words, mapping);
HashMap<String, Integer> dict = new HashMap<String, Integer> ();
for(int i=0; i<words.length; ++i) {
dict.put(words[i], i);
}
Iterator iter = mapping.entrySet().iterator();
while(iter.hasNext()) {
HashMap.Entry entry = (HashMap.Entry) iter.next();
String str = (String) entry.getKey();
int lpos = dict.get(str);
//System.out.print(str + " ==> ");
HashSet<String> hs = (HashSet<String>) entry.getValue();
Iterator<String> itc = hs.iterator();
while(itc.hasNext()) {
String s = itc.next();
//System.out.print(s + " ");
if(dict.containsKey(s)) {
int rpos = dict.get(s);
if(lpos != rpos) {
StringBuffer sb1 = new StringBuffer(str).append(s);
if(isPalindrome(sb1)) {
List<Integer> ll = new ArrayList<Integer> ();
ll.add(lpos);
ll.add(rpos);
hres.add(ll);
}
StringBuffer sb2 = new StringBuffer(s).append(str);
if(isPalindrome(sb2)) {
List<Integer> ll2 = new ArrayList<Integer> ();
ll2.add(rpos);
ll2.add(lpos);
hres.add(ll2);
}
}
}
}//System.out.println();
}
res.addAll(hres);
return res;
}
}
leetcode@ [336] Palindrome Pairs (HashMap)的更多相关文章
- LeetCode 336. Palindrome Pairs
原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...
- 【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 ...
随机推荐
- !!无须定义配置文件中的每个变量的读写操作,以下代码遍历界面中各个c#控件,自动记录其文本,作为配置文件保存
namespace PluginLib{ /// <summary> /// 遍历控件所有子控件并初始化或保存其值 /// </summary> pub ...
- HBase的Shell操作
1.进入命令行 bin/hbase shell 2.输入help 查看各种命令组. 命令是分组的,可以执行help 'general'查看general组的命令. 3.常用命令 --显示有哪些表 li ...
- HDU 3949 XOR(高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题意:给出一个长度为n的数列A.选出A的所有子集(除空集外)进行抑或得到2^n-1个数字,去重排 ...
- Android开发之获取状态栏高度、屏幕的宽和高
转自:http://blog.csdn.net/guolin_blog/article/details/16919859 获取状态栏的高度. private static int statusBarH ...
- eclipse导入javax.servlet.*的方法
1.下载web应用服务器tomact,网址http://tomcat.apache.org/download-80.cgi 这个根据自己系统进行选择. 2.将其加压到电脑中 3.在eclipse中添加 ...
- git push提示或错误
当 git 和 gerrit 一起使用的时候,你创建了一个 tag,现在需要 push 到远程仓库,当你没有权限的时候,会出现如下提示: $ git push origin v20150203 Tot ...
- 【转载】Java垃圾回收内存清理相关(虚拟机书第三章),GC日志的理解,CPU时间、墙钟时间的介绍
主要看<深入理解Java虚拟机> 第三张 P84 开始是垃圾收集相关. 1. 1960年诞生于MIT的Lisp是第一门采用垃圾回收的语言. 2. 程序计数器.虚拟机栈.本地方法栈3个区域随 ...
- HDU 4893 线段树
比赛时太大意,斐波拉契数列开小了. 题目大意:1个序列,3种操作,改变序列某个数大小,将序列中连续的一段每个数都变成其最近的斐波拉契数,以及查询序列中某一段的数之和. 解题思路:维护add[]数组表示 ...
- bzoj4025
首先我们要知道,怎么去维护一个是否是二分图 二分图的充要条件:点数>=2且无奇环 重点就是不存在奇环,怎么做呢 考虑随便维护一个图的生成树,不难发现,如果一条边加入后,形成奇环的话就不是二分图 ...
- UVa 1638 (递推) Pole Arrangement
很遗憾,这么好的一道题,自己没想出来,也许太心急了吧. 题意: 有长度为1.2.3...n的n个杆子排成一行.问从左到右看能看到l个杆子,从右往左看能看到r个杆子,有多少种排列方法. 分析: 设状态d ...