当前位置:网站首页>Force buckle 1790 Can two strings be equal by performing string exchange only once
Force buckle 1790 Can two strings be equal by performing string exchange only once
2022-07-07 20:06:00 【Tomorrowave】
1790. Can performing only one string exchange make two strings equal
Give you two strings of equal length s1 and s2 . once String swapping The operation steps are as follows : Select two subscripts in a string ( It doesn't have to be different ), And exchange the characters corresponding to the two subscripts .
If the One of the strings perform At most one string exchange You can make two strings equal , return true ; otherwise , return false .
Example 1:
Input :s1 = “bank”, s2 = “kanb”
Output :true
explain : for example , In exchange for s2 The first and last characters in can get “bank”
Ideas :
Matching algorithm idea . string matching
Code section
class Solution:
def areAlmostEqual(self, s1: str, s2: str) -> bool:
s1,s2=list(s1),list(s2)
if len(s1)!=len(s2):
return False
cnt=[]
for i in range(len(s1)):
if s1[i]!=s2[i] and len(cnt)<2 :
cnt.append(i)
if len(cnt)==2:
s1[cnt[0]],s1[cnt[1]]= s1[cnt[1]],s1[cnt[0]]
return s1==s2
边栏推荐
- R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
- Time tools
- ASP. Net learning & ASP's one word
- PMP每日一练 | 考试不迷路-7.7
- R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
- Implement secondary index with Gaussian redis
- el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
- Kubernetes——kubectl命令行工具用法详解
- JVM GC垃圾回收简述
- 第二十章 使用工作队列管理器(三)
猜你喜欢
PMP practice once a day | don't get lost in the exam -7.7
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
Le PGR est - il utile au travail? Comment choisir une plate - forme fiable pour économiser le cœur et la main - d'œuvre lors de la préparation de l'examen!!!
Ways to improve the utilization of openeuler resources 01: Introduction
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
微信公众号OAuth2.0授权登录并显示用户信息
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
ASP. Net learning & ASP's one word
干货分享|DevExpress v22.1原版帮助文档下载集合
随机推荐
831. KMP字符串
力扣 88.合并两个有序数组
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
How to buy bank financial products? Do you need a bank card?
小试牛刀之NunJucks模板引擎
Introduction to bit operation
力扣 1037.有效的回旋镖
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
PMP practice once a day | don't get lost in the exam -7.7
Equals method
JVM GC garbage collection brief
微信公众号OAuth2.0授权登录并显示用户信息
关于自身的一些安排
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
gorilla官方:golang开websocket client的示例代码
力扣 2319. 判断矩阵是否是一个 X 矩阵
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
LeetCode_7_5
R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the dot strip plot, set the position parameter, and configure the separation degree of different grouped
[auto.js] automatic script