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. Linux文件操作命令并举例说明其作用

    ls ,常用于查看当前文件下有工作中需要的文件 cd, 常用于进行切换文件的位置 vim,常用于编辑软件系统相关的配置文件 ps –ef|grep jdk,常用语显示跟jdk有关的进程   |:表示 ...

  2. 单口RAM、双口RAM、FIFO

    单口与双口的区别在于,单口只有一组数据线与地址线,因此读写不能同时进行.而双口有两组数据线与地址线,读写可同时进行.FIFO读写可同时进行,可以看作是双口.    双口RAM分伪双口RAM(Xilin ...

  3. 如何将Altera官方提供的CADENCE.OLB应用于altium Designer中

  4. 动手实现--AC自动机

    Trie树: 把若干个单词按前缀合并就得到一棵树,这棵树称为Trie树.Trie树是有根树,每条边表示一个字符,每个节点表示一个从根到当前节点的唯一路径上的字符依次连接得到的字符串.由于空串是任何串的 ...

  5. python的进栈出栈遍历

    python实现出栈进栈 要求: 进栈 出栈 遍历所有 退出 stack = [] #创建列表 #进栈 def pushstack(): stack.append(input('Enter a nub ...

  6. codingame

    无聊登了一下condingame,通知说有本周谜题,正好刚撸完bfs,想尝试下. 题目链接:https://www.codingame.com/ide/17558874463b39b9ce6d4207 ...

  7. Spring全家桶之spring boot(三)

    spring boot集成mybatis 众所周知,spring与springmvc可以无缝集成,而mybatis不是spring旗下的框架,因此需要进行配置,当然,这里的配置也是非常简单的. 1.首 ...

  8. Antd 表格数据分页展示

    分页组件代码 render(){ const {total,size,currenPage} = this.state // 参数分别为数据总条数.每页数据条数.当前页页码 return ( // 渲 ...

  9. 10.2 Go redis

    10.2 Go redis redis是NoSQL数据, 不是传统的关系型数据库.linux,windows环境皆可安装. https://redis.io http://www.redis.cn r ...

  10. 7.2 Go type assertion

    7.2 Go type assertion 类型断言是使用在接口值上的操作. 语法x.(T)被称为类型断言,x代表接口的类型,T代表一个类型检查. 类型断言检查它操作对象的动态类型是否和断言类型匹配. ...