当前位置:网站首页>力扣 1790. 仅执行一次字符串交换能否使两个字符串相等
力扣 1790. 仅执行一次字符串交换能否使两个字符串相等
2022-07-07 17:53:00 【Tomorrowave】
1790. 仅执行一次字符串交换能否使两个字符串相等
给你长度相等的两个字符串 s1 和 s2 。一次 字符串交换 操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。
如果对 其中一个字符串 执行 最多一次字符串交换 就可以使两个字符串相等,返回 true ;否则,返回 false 。
示例 1:
输入:s1 = “bank”, s2 = “kanb”
输出:true
解释:例如,交换 s2 中的第一个和最后一个字符可以得到 “bank”
思路:
匹配算法思路。字符串匹配
代码部分
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
边栏推荐
- 使用高斯Redis实现二级索引
- 一张图深入的理解FP/FN/Precision/Recall
- R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
- Visual Studio 插件之CodeMaid自动整理代码
- torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record
- 论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
- Throughput
- YoloV6:YoloV6+Win10---训练自己得数据集
- 项目经理『面试八问』,看了等于会了
- Welcome to the markdown editor
猜你喜欢
Kirin Xin'an joins Ningxia commercial cipher Association
2022如何评估与选择低代码开发平台?
Detailed explanation of Flink parallelism and slot
华南X99平台打鸡血教程
Make this crmeb single merchant wechat mall system popular, so easy to use!
开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!
CSDN syntax description
Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记
LeetCode_7_5
PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
随机推荐
mock.js从对象数组中任选数据返回一个数组
力扣 459. 重复的子字符串
ant desgin 多选
How to cooperate among multiple threads
关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
8 CAS
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
841. 字符串哈希
equals 方法
Cloud 组件发展升级
Kirin Xin'an cloud platform is newly upgraded!
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
开源OA开发平台:合同管理使用手册
openEuler 资源利用率提升之道 01:概论
MSE API学习
强化学习-学习笔记8 | Q-learning
tp6 实现佣金排行榜
Implement secondary index with Gaussian redis