当前位置:网站首页>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
边栏推荐
猜你喜欢
LeetCode_7_5
开源OA开发平台:合同管理使用手册
[RT thread env tool installation]
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
剑指 Offer II 013. 二维子矩阵的和
力扣 2319. 判断矩阵是否是一个 X 矩阵
openEuler 资源利用率提升之道 01:概论
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
ASP. Net learning & ASP's one word
关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
随机推荐
PMP每日一练 | 考试不迷路-7.7
Equals method
sql 常用优化
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!!!
力扣 2319. 判断矩阵是否是一个 X 矩阵
Boot 和 Cloud 的版本选型
MRS离线数据分析:通过Flink作业处理OBS数据
Semantic SLAM源码解析
YoloV6:YoloV6+Win10---训练自己得数据集
Implement secondary index with Gaussian redis
R language ggplot2 visualization: use the ggdensity function of ggpubr package to visualize the packet density graph, and use stat_ overlay_ normal_ The density function superimposes the positive dist
Cuda版本不一致,编译apex报错
强化学习-学习笔记8 | Q-learning
一锅乱炖,npm、yarn cnpm常用命令合集
UCloud是基础云计算服务提供商
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
BI的边界:BI不适合做什么?主数据、MarTech?该如何扩展?
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
MySQL、sqlserver oracle数据库连接方式
Chapter 20 using work queue manager (3)