当前位置:网站首页>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
边栏推荐
- Sword finger offer II 013 Sum of two-dimensional submatrix
- 开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!
- 线性基
- 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
- 力扣 2319. 判断矩阵是否是一个 X 矩阵
- 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
- 【STL】vector
- Introduction to bit operation
- Cuda版本不一致,编译apex报错
- Throughput
猜你喜欢
vulnhub之school 1
力扣 2319. 判断矩阵是否是一个 X 矩阵
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
多个线程之间如何协同
Sword finger offer II 013 Sum of two-dimensional submatrix
PMP practice once a day | don't get lost in the exam -7.7
YoloV6:YoloV6+Win10---训练自己得数据集
Implement secondary index with Gaussian redis
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
随机推荐
Simulate the implementation of string class
PMP practice once a day | don't get lost in the exam -7.7
R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
Throughput
银行理财产品怎么买?需要办银行卡吗?
Flink并行度和Slot详解
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!!!
Tp6 realize Commission ranking
Compiler optimization (4): inductive variables
【STL】vector
tp6 实现佣金排行榜
CSDN syntax description
equals 方法
【STL】vector
力扣 1037.有效的回旋镖
841. String hash
JVM class loading mechanism
JVM 类加载机制
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
LC: string conversion integer (ATOI) + appearance sequence + longest common prefix