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++:派生类的构造函数和析构函数
4.2 派生类的构造函数和析构函数4.2.1 派生类构造函数和析构函数的执行顺序 通常情况下,当创建派生类对象时,首先执行基类的构造函数,随后再执行派生类的构造函数:当撤销派生类对象时,则先执行派生类 ...
- 计算机技能get(windows系统)
1.快速打开程序,比如计算器,注册表,先按win键(不用再按win+r啦),输入程序名字,如calc,regedit等,直接打开. 2.自动左右分屏,win+上下左右方向键,win+↑ 最大化,win ...
- 一行很好的JS代码
[].forEach.call($$("*"),function(a){ a.style.outline="1px solid #"+(~~(Math.rand ...
- BZOJ 2440 完全平方数(莫比乌斯-容斥原理)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2440 题意:给定K.求不是完全平方数(这里1不算完全平方数)的倍数的数字组成的数字集合S ...
- [Codeforces670A]Holidays(数学,构造)
题目链接:http://codeforces.com/contest/670/problem/A 题意:给n天,问这n天最少和最多有多少个休息日,不用区分平闰年. 这题写几个例子,YY一下就构造出来解 ...
- JAVA将Excel中的报表导出为图片格式(一)问题背景
如题所示,先抛出一个问题,如何使用JAVA将Excel中的报表导出为图片格式? 首先说一下这个问题的背景,也就是为什么博主会碰到这个问题 随着微信,易信之流大行其道,企业内部的办公交流.绩效考评甚至考 ...
- bzoj2791
每个顶点有且仅有一条出边是什么意思呢 类似一棵树,树上的边都是由儿子指向父亲的,并且这个东西带着一个环 也就是一个个有向环套有向树…… 这题还是比较简单的,把环作为根然后类似lca做即可,注意细节的p ...
- UVa 11427 (期望 DP) Expect the Expected
设d(i, j)表示前i局每局获胜的比例均不超过p,且前i局共获胜j局的概率. d(i, j) = d(i-1, j) * (1-p) + d(i-1, j-1) * p 则只玩一天就就不再玩的概率Q ...
- Lambda表达式【转】
lambda表达式是对匿名方法的一种改进,具有更加简洁的语法和更易理解的形式,lambda表达式可以包括表达式和语句,并且可以用与创建委托或表达式目录树类型. lambda表达式都使用Lambda运算 ...
- Asp.Net读写XML简单方法
xml文件 <?xml version="1.0" encoding="utf-8"?> <book> <title>web ...