当前位置:网站首页>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
边栏推荐
猜你喜欢

PMP practice once a day | don't get lost in the exam -7.7

使用高斯Redis实现二级索引

9 atomic operation class 18 Rohan enhancement

Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!

Sword finger offer II 013 Sum of two-dimensional submatrix

华南X99平台打鸡血教程

模拟实现string类

Simulate the implementation of string class

Cloud 组件发展升级

vulnhub之school 1
随机推荐
R语言dplyr包mutate_at函数和min_rank函数计算dataframe中指定数据列的排序序号值、名次值、将最大值的rank值赋值为1
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
SQL common optimization
Some important knowledge of MySQL
多个线程之间如何协同
Browse the purpose of point setting
Mysql, sqlserver Oracle database connection mode
位运算介绍
IP 工具类
Flink并行度和Slot详解
pom.xml 配置文件标签作用简述
How to buy bank financial products? Do you need a bank card?
Kubernetes——kubectl命令行工具用法详解
MySQL、sqlserver oracle数据库连接方式
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
Ucloud is a basic cloud computing service provider
R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize the violin diagram, set the palette parameter to customize the filling color of violin diagrams at different
Openeuler prize catching activities, to participate in?
A pot of stew, a collection of common commands of NPM and yarn cnpm
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表