当前位置:网站首页>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
边栏推荐
- 开源OA开发平台:合同管理使用手册
- PMP每日一练 | 考试不迷路-7.7
- JVM GC garbage collection brief
- LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
- mock. JS returns an array from the optional data in the object array
- openEuler 资源利用率提升之道 01:概论
- MSE API学习
- ASP. Net learning & ASP's one word
- vulnhub之Funfox2
- Notes...
猜你喜欢
随机推荐
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
第二十章 使用工作队列管理器(三)
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
gorilla官方:golang开websocket client的示例代码
力扣 1790. 仅执行一次字符串交换能否使两个字符串相等
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
一锅乱炖,npm、yarn cnpm常用命令合集
MIT科技评论文章:围绕Gato等模型的AGI炒作可能使人们忽视真正重要的问题
Ucloud is a basic cloud computing service provider
Visual Studio 插件之CodeMaid自动整理代码
My creation anniversary
[confluence] JVM memory adjustment
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
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
UCloud是基础云计算服务提供商
openEuler 资源利用率提升之道 01:概论
Semantic SLAM源码解析
tp6 实现佣金排行榜