当前位置:网站首页>336. Palindromic Pairs
336. Palindromic Pairs
2022-07-29 21:38:00 【"We want to brush KouTi】
前言
给定一组 互不相同 的单词, 找出所有 不同 的索引对 (i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串.
示例 1:
输入:words = [“abcd”,“dcba”,“lls”,“s”,“sssll”]
输出:[[0,1],[1,0],[3,2],[2,4]]
解释:可拼接成的回文串为 [“dcbaabcd”,“abcddcba”,“slls”,“llssssll”]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/palindrome-pairs
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
解题思路
raw string intohash表
找前缀
找后缀
So try all cases where the prefix is a palindrome-遍,If the suffix is a palindrome, try it again

边界
In the case of a palindrome as a whole, empty strings are spelled before and after
own reverse order
代码
class Solution {
public static List<List<Integer>> palindromePairs(String[] words) {
HashMap<String, Integer> wordset = new HashMap<>();
for (int i = 0; i < words.length; i++) {
wordset.put(words[i], i);
}
List<List<Integer>> res = new ArrayList<>();
//{ [6,23] 、 [7,13] }
for (int i = 0; i < words.length; i++) {
// i words[i]
// findAll(字符串,在i位置,wordset) 返回所有生成的结果返回
res.addAll(findAll(words[i], i, wordset));
}
return res;
}
public static List<List<Integer>> findAll(String word, int index, HashMap<String, Integer> words) {
List<List<Integer>> res = new ArrayList<>();
String reverse = reverse(word);
Integer rest = words.get("");
if (rest != null && rest != index && word.equals(reverse)) {
addRecord(res, rest, index);
addRecord(res, index, rest);
}
int[] rs = manacherss(word);
int mid = rs.length >> 1;
for (int i = 1; i < mid; i++) {
if (i - rs[i] == -1) {
rest = words.get(reverse.substring(0, mid - i));
if (rest != null && rest != index) {
addRecord(res, rest, index);
}
}
}
for (int i = mid + 1; i < rs.length; i++) {
if (i + rs[i] == rs.length) {
rest = words.get(reverse.substring((mid << 1) - i));
if (rest != null && rest != index) {
addRecord(res, index, rest);
}
}
}
return res;
}
public static void addRecord(List<List<Integer>> res, int left, int right) {
List<Integer> newr = new ArrayList<>();
newr.add(left);
newr.add(right);
res.add(newr);
}
public static int[] manacherss(String word) {
char[] mchs = manachercs(word);
int[] rs = new int[mchs.length];
int center = -1;
int pr = -1;
for (int i = 0; i != mchs.length; i++) {
rs[i] = pr > i ? Math.min(rs[(center << 1) - i], pr - i) : 1;
while (i + rs[i] < mchs.length && i - rs[i] > -1) {
if (mchs[i + rs[i]] != mchs[i - rs[i]]) {
break;
}
rs[i]++;
}
if (i + rs[i] > pr) {
pr = i + rs[i];
center = i;
}
}
return rs;
}
public static char[] manachercs(String word) {
char[] chs = word.toCharArray();
char[] mchs = new char[chs.length * 2 + 1];
int index = 0;
for (int i = 0; i != mchs.length; i++) {
mchs[i] = (i & 1) == 0 ? '#' : chs[index++];
}
return mchs;
}
public static String reverse(String str) {
char[] chs = str.toCharArray();
int l = 0;
int r = chs.length - 1;
while (l < r) {
char tmp = chs[l];
chs[l++] = chs[r];
chs[r--] = tmp;
}
return String.valueOf(chs);
}
}
边栏推荐
猜你喜欢

怎么实现您的个人知识库?

分析少年派2中的Crypto

人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准

如何进入董事会:给CIO的十条建议

诺氟沙星-DNA复合物|半乳糖化脂质体-聚阳离子-DNA复合物|注意事项

In the past six months, I have done those things about the automatic return of the transaction link...

JUC Concurrent Programming Basics AQS

SAP ABAP OData 服务 Data Provider Class 的 GET_ENTITYSET 方法实现指南试读版

R language for airbnb data nlp text mining, geography, word cloud visualization, regression GAM model, cross-validation analysis

VR直播营销需求增加,数据模块为我们铺路
随机推荐
海量数据查询方案mysql_Mysql海量数据存储和解决方案之二—-Mysql分表查询海量数据…[通俗易懂]
The sword refers to Offer II 097. Number of subsequences
Looking for a job - a chat with my cousin
:style中颜色使用函数动态获取赋值
手写dialog弹框
LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路
GET_ENTITYSET Method Implementation Guide for SAP ABAP OData Service Data Provider Class
优惠券系统设计思想
2022了你还不会『低代码』?数据科学也能玩转Low-Code啦!
微信小程序 31 分包机制
es6语法使用默认参数和解构
打破原则!MongoDB 引入 SQL?
1. Promise usage in JS, 2. The concept and usage of closures, 3. The difference between the four methods and areas of object creation, 4. How to declare a class
双功能RGD-TAT修饰DNA纳米胶束|聚苯胺纳米线修饰DNA(PAINW/DNA)
GalNAc-siRNA甘露糖/半乳糖修饰脱氧核糖核酸|siRNA-S-S-DSPE(RNA修饰技术介绍)
PEG-PEI共聚物/DNA复合物|甘草次酸修饰的长循环阳离子脂质体DNA复合物|解析说明
LeetCode--single linked list--146.LRU cache
R语言对airbnb数据nlp文本挖掘、地理、词云可视化、回归GAM模型、交叉验证分析
offsetwidth111[easy to understand]
全景教程丨VR全景拍摄如何拍摄日出和日落的场景?