当前位置:网站首页>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
边栏推荐
- Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
- BI的边界:BI不适合做什么?主数据、MarTech?该如何扩展?
- 使用高斯Redis实现二级索引
- 模拟实现string类
- torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
- [RT thread env tool installation]
- 力扣599. 两个列表的最小索引总和
- CSDN语法说明
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- 力扣 1037.有效的回旋镖
猜你喜欢
随机推荐
R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
pom. XML configuration file label: differences between dependencies and dependencymanagement
841. 字符串哈希
8 CAS
torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record
Kubernetes——kubectl命令行工具用法详解
831. KMP字符串
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
IP tools
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
最多可以参加的会议数目[贪心 + 优先队列]
Boot 和 Cloud 的版本选型
干货分享|DevExpress v22.1原版帮助文档下载集合
Training IX basic configuration of network services
使用高斯Redis实现二级索引
Version selection of boot and cloud
编译器优化那些事儿(4):归纳变量
剑指 Offer II 013. 二维子矩阵的和
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线








