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 PalindromePairs {
public boolean isPalindrome(String string) {
char c[]=string.toCharArray();
boolean flag=true;
for (int i = 0; i < c.length/2; i++) {
if (c[i]!=c[c.length-i-1]) {
flag=false;
break;
}
}
return flag;
}
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> b= new ArrayList<List<Integer>>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words.length; j++) {
if (i!=j) {
String string=words[i]+words[j];
if (isPalindrome(string)) {
List<Integer> a= new ArrayList<Integer>();
a.add(i);
a.add(j);
b.add(a);
}
}
}
}
return b;
}
public static void main(String[] args) {
String [] words={"hijajcd","hjhgahbcegj"};
PalindromePairs pairs=new PalindromePairs();
System.out.println(pairs.palindromePairs(words));
}
}

方法二:

利用

public class Solution {
public List<List<Integer>> palindromePairs(String[] words) {
List<List<Integer>> resultList= new ArrayList<List<Integer>>();
Map<String,Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < words.length; i++) {
map.put(words[i], i);
}
for (int i = 0; i < words.length; i++) {
for (int j = 0; j <= words[i].length(); j++) {
String subString=words[i].substring(0,j);
String subString2=words[i].substring(j);
if (isPalindrome(subString)) {
if (map.get(reverseString(subString2))!=null) {
ArrayList<Integer> arrayList=new ArrayList<Integer>(2);
arrayList.add(map.get(reverseString(subString2)));
arrayList.add(map.get(words[i]));
if (!arrayList.get(0).equals(arrayList.get(1))) {
resultList.add(arrayList);
}
}
}
if (isPalindrome(subString2)) {
if (map.get(reverseString(subString))!=null) {
ArrayList<Integer> arrayList=new ArrayList<Integer>(2);
arrayList.add(map.get(words[i]));
arrayList.add(map.get(reverseString(subString)));
if (!arrayList.get(0).equals(arrayList.get(1))) {
resultList.add(arrayList);
}
}
}
}
}
HashSet hashSet=new HashSet(resultList);
resultList.clear();
resultList.addAll(hashSet);
return resultList; }
public boolean isPalindrome(String string) {
char c[]=string.toCharArray();
boolean flag=true;
for (int i = 0; i < c.length/2; i++) {
if (c[i]!=c[c.length-i-1]) {
flag=false;
break;
}
}
return flag;
} public String reverseString(String string) {
char[] chars=string.toCharArray();
char[] result=new char[chars.length];
for (int i = 0; i < chars.length; i++) {
result[chars.length-i-1]=chars[i];
}
return new String(result);
}
}

Palindrome Pairs的更多相关文章

  1. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  2. LeetCode 336. Palindrome Pairs

    原题链接在这里:https://leetcode.com/problems/palindrome-pairs/ 题目: Given a list of unique words, find all p ...

  3. 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 ...

  4. 【题解】Palindrome pairs [Codeforces159D]

    [题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...

  5. leetcode 132 Palindrome Pairs 2

    lc132 Palindrome Pairs 2 大致与lc131相同,这里要求的是最小分割方案 同样可以分割成子问题 dp[i][j]还是表示s(i~j)是否为palindrome res[i]则用 ...

  6. leetcode 131 Palindrome Pairs

    lc131 Palindrome Pairs 解法1: 递归 观察题目,要求,将原字符串拆成若干子串,且这些子串本身都为Palindrome 那么挑选cut的位置就很有意思,后一次cut可以建立在前一 ...

  7. 【LeetCode】336. Palindrome Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...

  8. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  9. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  10. Palindrome Pairs -- LeetCode 336

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

随机推荐

  1. petapoco存储过程

    db.ExecuteScalar<string>("exec P_GetCode @0,@1,@2,@3,@4,@5",); using (var db = new D ...

  2. php 教程列表

    php教程 php概述 php环境搭建 PHP书写格式 php变量 php常量 PHP注释 php字符串 string PHP整型 integer PHP浮点型 float php布尔型 php数据类 ...

  3. echo同时输出到多个文件中

    echo "test"| tee -a file1 file2 如果想去掉在屏幕上的显示 echo "test"| tee -a file1 file2 > ...

  4. Jmeter模拟不同带宽

    Jmeter自带模拟带宽设置,当然前提肯定是你当前的带宽>=你要模拟的带宽,好比你装了个4m的宽带,要模拟100m的带宽,那是做梦 做起来也不难,打开user.properties文件,增加如下 ...

  5. 可变参数宏__VA_ARGS__和...

    __VA_ARGS__ 是一个可变参数的宏(gcc支持).实现思想就是宏定义中参数列表的最后一个参数为省略号(也就是三个点).这样预定义宏_ _VA_ARGS_ _就可以被用在替换部分中,替换省略号所 ...

  6. C# 计算两个字符串的相似度

    我们在做数据系统的时候,经常会用到模糊搜索,但是,数据库提供的模糊搜索并不具备按照相关度进行排序的功能. 现在提供一个比较两个字符串相似度的方法. 通过计算出两个字符串的相似度,就可以通过Linq在内 ...

  7. MongoDB学习笔记(入门)

    一.文档的注意事项:1.  键值对是有序的,如:{ "name" : "stephen", "genda" : "male&quo ...

  8. javascript判断iphone/android手机横竖屏模式的函数

    function orientationChange(){ switch(window.orientation) { case 0: // Portrait case 180: // Upside-d ...

  9. Life cycle of plist in Lockdown changes dramatically in iOS 10

    We could take advantage of plist to bypass Trust Relationship so as to extract data from a iDevice. ...

  10. FusionCharts中仪表盘相关属性

    上上周用FusionCharts做了几个报表,里面有个仪表盘,当时查属性查疯了,现在把相关的一些属性记下来,方便以后查找. -------------------------仪表盘重要属性解析---- ...