336. 回文对

给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。

示例 1:

输入: [“abcd”,“dcba”,“lls”,“s”,“sssll”]

输出: [[0,1],[1,0],[3,2],[2,4]]

解释: 可拼接成的回文串为 [“dcbaabcd”,“abcddcba”,“slls”,“llssssll”]

示例 2:

输入: [“bat”,“tab”,“cat”]

输出: [[0,1],[1,0]]

解释: 可拼接成的回文串为 [“battab”,“tabbat”]

PS:

字符串反转构建字典树,再判定当前字符串在字典树中是否存在,如存在则证明存在其他串的子串镜像和该串相等,定义子串平均长度为k,则复杂度为O(N * K ^ 2)

class Solution {
private static List<List<Integer>> ans = new ArrayList<>(); public List<List<Integer>> palindromePairs(String[] words) {
if (words == null || words.length == 0) {
return null;
} ans = new ArrayList<>();
TrieNode root = new TrieNode(); for (int i = 0; i < words.length; i++) {
addWord(root, words[i], i);
} for (int i = 0; i < words.length; i++) {
find(root, words[i], i);
}
return ans;
} private static class TrieNode {
//当前字符串的数组位置,下游节点,以及能构成当前串or回文子串节点的数组位置集合
int index;
TrieNode[] next;
List<Integer> palindIndex; public TrieNode() {
index = -1;
next = new TrieNode[26];
palindIndex = new ArrayList<>();
}
} private static void addWord(TrieNode root, String word, int index) {
for (int i = word.length() - 1; i >= 0; i--) {
int ch = word.charAt(i) - 'a';
if (root.next[ch] == null) {
root.next[ch] = new TrieNode();
} if (isPalindrome(word, 0, i)) {
root.palindIndex.add(index);
}
root = root.next[ch];
}
root.index = index;
root.palindIndex.add(index);
} private static void find(TrieNode root, String word, int index) {
for (int i = 0; i < word.length(); i++) {
//待匹配串比字典树子串长,如asadcc匹配树上的asad
if (root.index != -1 && root.index != index && isPalindrome(word, i, word.length() - 1)) {
ans.add(Arrays.asList(index, root.index));
}
//System.out.println("root index:" + root.index);
if (root.next[word.charAt(i) - 'a'] == null) {
return;
}
root = root.next[word.charAt(i) - 'a'];
}
//待匹配串比字典树子串短,如asad匹配树上的asadcc
for (int i : root.palindIndex) {
if (i != index) {
ans.add(Arrays.asList(index, i));
}
}
} private static boolean isPalindrome(String string, int l, int r) {
while (l < r) {
if (string.charAt(l++) != string.charAt(r--)) {
return false;
}
}
return true;
}
}

Java实现 LeetCode 336 回文对的更多相关文章

  1. Java实现 LeetCode 9 回文数

    9. 回文数 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false ...

  2. Leetcode 336.回文对

    回文对 给定一组唯一的单词, 找出所有不同 的索引对(i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串. 示例 1: 输入: ["abcd&quo ...

  3. Java实现 LeetCode 647 回文子串(暴力)

    647. 回文子串 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串. 示例 1: 输入: "a ...

  4. Java实现 LeetCode 234 回文链表

    234. 回文链表 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否 ...

  5. leetcode 1.回文数-(easy)

    2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...

  6. java判断字符串是否回文

    java判断字符串是否回文 /** * java判断字符串是否回文<br><br> * 基本思想是利用字符串首尾对应位置相比较 * * @author InJavaWeTrus ...

  7. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  8. [LeetCode]9.回文数(Java)

    原题地址: palindrome-number 题目描述: 给你一个整数 x ,如果 x 是一个回文整数,返回 true :否则,返回 false . 回文数是指正序(从左向右)和倒序(从右向左)读都 ...

  9. LeetCode 647. 回文子串(Palindromic Substrings)

    647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符 ...

随机推荐

  1. 设计模式之GOF23桥接模式

    桥接模式 当一个问题违反单一职责原则时,及控制该产品的有多个维度,为了扩展时减少类的膨胀个数,可以选用桥接模式 避免多重继承时 例如买电脑时                  桥接模式

  2. JDBC04 PreparedStatement

    PreparedStatement类 存在预编译,用占位符去填参数(参数索引从1开始算),可以防止SQL注入 try { Class.forName("com.mysql.cj.jdbc.D ...

  3. html5 canvas 绘制上下浮动的字体

    绘制上下浮动的字体主要思想为先绘制好需要的字体,每隔一定的时间将画布清空,然后再将字体位置改变再绘制上去 如此循环即可. (function(window) { var flowLogo = func ...

  4. 2018-06-19 Javascript 基础2

    js变量类型测试:typeof()->五种 (number,string,boolean,object,undefined): instanceof->检查某个对象是否是某个构造器产生的 ...

  5. 如何用尾插法建立双链表(C语言,非循环)

    如何用尾插法建立双链表 其实本来是想完成汪队给的链表快排的作业,但是我写完建立双链表以后就12点了龟龟,明天还要早起QAQ,我菜死了 一,为啥要有双链表 先说单链表吧单链表长这样 他的一个结点结构就是 ...

  6. 解决 es CircuitBreakingException 问题

    比如频繁报如下错误, [2019-06-16T15:31:22,778][DEBUG][o.e.a.a.c.n.i.TransportNodesInfoAction] [node-xxx] faile ...

  7. python中的基础坑

    v = [lambda :x for x in range(10)] print(v) #[lambda :x,lambda :x....]10个匿名函数 print(v[0]) #lambda :x ...

  8. 14.2 Go性能优化

    14.2 Go性能优化 优化手段 1.减少HTTP请求数,合并CSS.JS.图片 2.使用CDN,就近访问 3.启用nginx gzip压缩,降低传输内容大小 4.优化后端api性能 api服务性能优 ...

  9. Null passed to a callee that requires a non-null argument

    OC中定义的方法参数默认是不为空的,如果能够为空需要手动指定__nullable ,我想这个警告是提示开发者警惕可能空参数

  10. LightOJ1030 Discovering Gold

    题目链接:https://vjudge.net/problem/LightOJ-1030 知识点: 概率与期望 解题思路: 设某一个点 \(i\) 能到达的点的个数为 \(x\),其上有金 \(g\) ...