当前位置:网站首页>力扣 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
边栏推荐
- PMP practice once a day | don't get lost in the exam -7.7
- Introduction to bit operation
- vulnhub之school 1
- 吞吐量Throughout
- Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
- 实训九 网络服务的基本配置
- equals 方法
- The research group of the Hunan Organizing Committee of the 24th China Association for science and technology visited Kirin Xin'an
- R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
- 干货分享|DevExpress v22.1原版帮助文档下载集合
猜你喜欢
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
关于ssh登录时卡顿30s左右的问题调试处理
Interpretation of transpose convolution theory (input-output size analysis)
小试牛刀之NunJucks模板引擎
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
PMP每日一练 | 考试不迷路-7.7
CSDN syntax description
使用高斯Redis实现二级索引
8 CAS
随机推荐
【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
R language dplyr package mutate_ At function and min_ The rank function calculates the sorting sequence number value and ranking value of the specified data column in the dataframe, and assigns the ra
Compiler optimization (4): inductive variables
R语言使用ggplot2函数可视化需要构建泊松回归模型的计数目标变量的直方图分布并分析构建泊松回归模型的可行性
9 atomic operation class 18 Rohan enhancement
A pot of stew, a collection of common commands of NPM and yarn cnpm
项目经理『面试八问』,看了等于会了
what‘s the meaning of inference
openEuler 有奖捉虫活动,来参与一下?
pom.xml 配置文件标签作用简述
vulnhub之school 1
Nunjuks template engine
R language uses ggplot2 function to visualize the histogram distribution of counting target variables that need to build Poisson regression model, and analyzes the feasibility of building Poisson regr
Simulate the implementation of string class
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
MSE API学习
mysql 的一些重要知识
Ways to improve the utilization of openeuler resources 01: Introduction
JVM 类加载机制
凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展