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. 如何使用apt-get在ubuntu系统上安装OpenJDK 8

    文章目录 添加ppa仓库 设置openjdk版本 查看java 版本 Android 8.1 系统编译的时候需要安装OpenJDK 8,这里如果可以自己下载源码编译安装,当然本想编译Android系统 ...

  2. failed parsing overlays.

    clearn + rebuild + 重新运行: 删掉模拟器进程 + 重新运行:

  3. markdonwn 测试1

    标题测试 ## 二级标题 ### 三级标题 二级标题 三级标题 段落格式 换行 末尾两个空格 第一行第一行第一行第一行第一行第一行第一行第一行 第二行第二行第二行第二行第二行第二行第二行第二行 第一行 ...

  4. 使用Python创建一个系统监控程序--李渣渣(lizaza.cn)

    最近在做个人网站,但是由于服务器资源不足,偶尔会出现系统崩溃的现象,所以想写一个程序来实时监控系统状态.当系统资源占用过高时发送邮件提醒. psutil(进程和系统实用程序)是一个跨平台的库,用于检索 ...

  5. Codeforces 832D(Misha, Grisha and Underground,LCA)

    题意:在一棵生成树上,给出了三个点,求三个点之间最大的相交点数,CF难度1900. 题解:求出三个lca,并取深度最大的那个,就是我们要的三岔路口K,然后分别求出K到a,b,c三点的路径长度,取最大值 ...

  6. es 报错cannot allocate because allocation is not permitted to any of the nodes

    0.现象 es 集群报red ,有unassigned shared , 用命令 curl localhost:9200/_cat/shards |grep UNASSIGNED 可以查看. 即使你马 ...

  7. PostgreSQL 安装PYTHON扩展,访问页面或者第三方程序

    应用场景当数据库中relation表中有数据插入.更新.删除操作,postgresql 调用第三方接口,进行处理.这里用pgsq 中python的扩展插件来实现. 1.安装PostgreSQL中的Py ...

  8. linux常用命令---PATH与防火墙

    linux的路径PATH 防火墙

  9. Jenkins-插件开发-BUG-Messages类编译报错

    注意:下载Jenkins插件源码后报错Messages这个类怎么导包都报编译错误的问题! 今天从GitHub下载了Jenkins的一些插件源码,准备自己研究研究写个插件.但是发现每个源码中都存在一个编 ...

  10. jsp 循环数字

    <c:forEach var ="i" begin="1" end="${homeexamque.optionNum}" step=& ...