当前位置:网站首页>09 -- 回文对
09 -- 回文对
2022-06-23 11:49:00 【JH_Cao】
目录
一、什么是回文对
1. 回文对,就是对称的字符串,比如
"a"
"aba"
"aabb"
二、题目
给定一组 互不相同 的单词, 找出所有 不同 的索引对 (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"]
三、解题思路
回文+翻转
- 把words数组中所有的字符串先翻转一遍,然后再放到一个字典中[String: Int]
- 对words数组进行遍历
- 如果遍历到是空数组,并且字典中含有key是回文对的,那么就加入答案中
- 复杂度:O(n^2)
例如:
1. words翻转,加入字典,那么将会得到字典
[["dcba": 0], ["abcd": 1], ["sll": 2], ["s": 3], ["llsss": 4]]
2. 对数组words = ["abcd","dcba","lls","s","sssll"]进行遍历
(1) 举例当前下标为2,则字符串为“lls”
从下标为0开始,进行分割,
left = "" , right = "lls",虽然left为回文对,但 字典中没有“lls”为key,继续分割
left = "l", right = "ls", 虽然left为回文对,但字典中没有“ls”为key, 继续分割
left = "ll", right = "s", left回文对,字典中也包含“s”为key,当前下标index = 2
["s": 3]中的value = 3
因为对称的那一段(left),必须放中间,拼接起来的新字符串才可以也是回文对
所以当前下标的字符串放在后面
那么 [3 , 2] 下标组成的字符串为 “s” + "lls" == “slls”, 是回文对
(2) 举例当前下标为4,则字符串为“sssll”
从下标为0开始,进行分割,
left = "" , right = "sssll",虽然left为回文对,但 字典中没有“sssll”为key,继续分割
left = "s", right = "ssll", 虽然left为回文对,但字典中没有“ssll”为key, 继续分割
left = "ss", right = "sll", left回文对,字典中也包含“sll”为key,当前下标index = 4
["sll": 2]中的value = 2
同理:
因为对称的那一段(left),必须放中间,拼接起来的新字符串才可以也是回文对
所以当前下标的字符串放在后面
那么 [2, 4] => lls + sssll + = llssssll => 是回文对
(3)同理:
如果对称的那一段是(right)部分,那么当前下标的字符串要放前面
组合起来就是[ index, dict[right]]
代码:
//字典方法
func palindromePairs(_ words: [String]) -> [[Int]] {
var dict = [String: Int]()
var res = [[Int]]()
words.enumerated().forEach { (index, str) in
dict[String(str.reversed())] = index
}
for (index, s) in words.enumerated() {
if s.isEmpty {
dict.forEach { (Str, idx) in
if checkIsOK(Str) && index != idx {
res.append([index,idx])
}
}
}
for j in 0..<s.count {
let mid = s.index(s.startIndex, offsetBy: j)
let leftPart = String(s[..<mid])
let rightPart = String(s[mid...])
if checkIsOK(leftPart) && dict[rightPart] != nil && dict[rightPart] != index {
res.append([dict[rightPart]!, index])
}
if checkIsOK(rightPart) && dict[leftPart] != nil && dict[leftPart] != index {
res.append([index, dict[leftPart]!])
}
}
}
return res
}
func checkIsOK(_ s: String) -> Bool {
return s == String(s.reversed())
}两层for循环
逐个字符串相加,然后再尝试左右方向调整,拼接新字符串,判断新字符串是否是回文对,这种思路相对容易理解
复杂度: O(n^2)
代码:
// 拼接方法
func palindromePairsTimeout(_ words: [String]) -> [[Int]] {
let n = words.count
var res = [[Int]]()
for i in 0..<n-1 {
let str1 = words[i]
for j in i+1..<n {
let str2 = words[j]
if checkIsOK(str1 + str2) {
res.append([i,j])
}
if checkIsOK(str2 + str1) {
res.append([j,i])
}
}
}
return res
}
func checkIsOK(_ s: String) -> Bool {
return s == String(s.reversed())
}边栏推荐
- Use monotone stack to solve problems
- 16路HD-SDI光端机多路HD-SDI高清视频光端机16路3G-SDI高清音视频光端机
- Introduction and use of list
- 在工作中学习的三个方法
- Leetcode 1209. 删除字符串中的所有相邻重复项 II(初版本没过)
- More than observation | Alibaba cloud observable suite officially released
- 过采样系列三:量化误差与过采样率
- 汉源高科新一代绿色节能以太网接入工业交换机高效节能型千兆工业以太网交换机
- 直播带货app源码搭建中,直播CDN的原理是什么?
- 十大劵商如何开户?在线开户安全么?
猜你喜欢

Introduction to redis - Chapter 3 - data structures and objects - Dictionary

公开课丨玩的就是短视频!今晚教你精准变现!

Analysis of LinkedList source code

Redis 入门-第一篇-数据结构与对象-简单动态字符串(SDS)

学习笔记 scrapy 爬虫框架

L'outil de périphérique deveco aide au développement de périphériques openharmony

电脑坏了,换了台电脑,装node环境的时候出了问题,报错URL not defined

php 手写一个完美的守护进程

得物多活架构设计之路由服务设计

KDD 2022 | 基于分层图扩散学习的癫痫波预测
随机推荐
KDD 2022 | 基于分层图扩散学习的癫痫波预测
杜邦分析法解读:安阳钢铁股份有限公司企业投资价值何在?
Which securities company is the most reliable and safe to open an account
手机证券开户交易?现在网上开户安全么?
Surprise! Amd acquires Xilinx with USD 35billion!
PPT制作3D旋转动画从入门到进阶
在工作中学习的三个方法
【云舟说直播间】-数字安全专场明天下午正式上线
4路电话+1路千兆以太网4路PCM电话光端机
64路电话+2路千兆以太网64路PCM电话光端机语音电话转光纤
Deep analysis and Simulation of list
使用单调栈解题
Meta said that the UK security law may "scan all private information" or infringe privacy
Redis 入门-第四篇-数据结构与对象-跳跃表
Open classes are short videos! Tonight, I will teach you how to realize accurately!
Redis 入门-第三篇-数据结构与对象-字典
Ppt makes 3D rotation animation from beginner to advanced
成熟的知识管理,应具备哪些条件?
The computer broke down. I changed the computer. There was a problem installing the node environment. The URL is not defined
[cloud native & microservice viii] source code analysis of weightedresponsetimerule of ribbon load balancing strategy (response time weighting)