1. Description

  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"]

2. Answer  

public class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(words == null || words.length == 0){
return res;
}
//build the map save the key-val pairs: String - idx
HashMap<String, Integer> map = new HashMap<>();
for(int i = 0; i < words.length; i++){
map.put(words[i], i);
} //special cases: "" can be combine with any palindrome string
if(map.containsKey("")) {
int blankIdx = map.get("");
for(int i = 0; i < words.length; i++) {
if(isPalindrome(words[i])) {
if(i == blankIdx)
continue;
res.add(Arrays.asList(blankIdx, i));
res.add(Arrays.asList(i, blankIdx));
}
}
} //find all string and reverse string pairs
for(int i = 0; i < words.length; i++) {
String cur_r = reverseStr(words[i]);
if(map.containsKey(cur_r)) {
int found = map.get(cur_r);
if(found == i) continue;
res.add(Arrays.asList(i, found));
}
} //find the pair s1, s2 that
//case1 : s1[0:cut] is palindrome and s1[cut+1:] = reverse(s2) => (s2, s1)
//case2 : s1[cut+1:] is palindrome and s1[0:cut] = reverse(s2) => (s1, s2)
for(int i = 0; i < words.length; i++) {
String cur = words[i];
for(int cut = 1; cut < cur.length(); cut++) {
if(isPalindrome(cur.substring(0, cut))) {
String cut_r = reverseStr(cur.substring(cut));
if(map.containsKey(cut_r)) {
int found = map.get(cut_r);
if(found == i) continue;
res.add(Arrays.asList(found, i));
}
}
if(isPalindrome(cur.substring(cut))) {
String cut_r = reverseStr(cur.substring(0, cut));
if(map.containsKey(cut_r)){
int found = map.get(cut_r);
if(found == i) continue;
res.add(Arrays.asList(i, found));
}
}
}
} return res;
} public String reverseStr(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
} public 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;
}
}

【LeetCode】Palindrome Pairs(336)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

  4. 【leetcode】Course Schedule(middle)☆

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  5. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

  7. 【LeetCode】Self Crossing(335)

    1. Description You are given an array x of n positive numbers. You start at point (0,0) and moves x[ ...

  8. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  9. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

随机推荐

  1. 谢欣伦 - 原创软件 - 游戏专题 - 我的桌面My Desktop

    今天在网上看到一个用桌面背景当做拼图内容的游戏很新颖,反正今天下雨我也闲着,索性用了半天时间做了一个类似的游戏<MyDesktop>.做完后立即分享给了两个朋友,他俩都被吓坏了.现在分享给 ...

  2. Java的书写格式,标识符及命名规则,注释

    Java的书写格式,标识符及命名规则,注释 1.Java语言的书写格式(约定成俗) 1) 大括号要对齐(左大括号与句尾对其,后面大括号与句头对齐),并且成对写 2) 左大括号前面有空格 3) 遇到左大 ...

  3. HTML基础篇之视频音频

    <audio src="song.ogg" controls="controls"></audio> <!-- 兼容的音频格式og ...

  4. ListView.setAdapter(adapter);空指针异常的解决的总结

    报空指针异常一般的情况: 1,没有找到布局文件的ID  检验是不是id重复或者写错了 2.控件没有实例化 3.没有找到布局文件的id,要看看是不是加载了布局了,必须加载了对应的布局才能找到对应布局下的 ...

  5. HTML的两三事

    关于在页面布局上,我用了两种方式  用&nbsp来调整需要空格的地方,  用<pre></pre>直接在代码页面调整页面布局  但是用&nbsp来布局当我把网页 ...

  6. 启动App的Intent

    类似桌面图标打开App的Intent 程序中需要一种通知,点击后的效果需要像点击桌面图标那样: 程序在前台就什么也不干. 程序在后台,就切换到前台. 程序未启动,就启动程序. 点击通知后,通知本身跳转 ...

  7. CodeFirst实战:用文本数据库存档软件配置

    背景: 以前要写软件的时候,在编写用户配置这一块时,由于存档数据库不靠谱或大题小作,所以一般是存在文本中. 一开始是一个文件保存一个配置(图个File.Read与File.Write的操作简单) 由于 ...

  8. 我的LESS编译方案

    背景 近期项目前端决定使用less,简单介绍一下,详细信息有兴趣查看官方文档(http://www.lesscss.net/article/home.html) LESSCSS是一种动态样式语言,属于 ...

  9. 《Spark快速大数据分析》—— 第五章 数据读取和保存

    由于Spark是在Hadoop家族之上发展出来的,因此底层为了兼容hadoop,支持了多种的数据格式.如S3.HDFS.Cassandra.HBase,有了这些数据的组织形式,数据的来源和存储都可以多 ...

  10. Sublime Text3 配置 NodeJs 环境

    前言 大家都知道,Sublime Text 安装插件一般从 Package Control 中直接安装即可,当我安装 node js 插件时候,直接通过Package Control 安装,虽然插件安 ...