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

开源OA开发平台:合同管理使用手册
![[RT thread env tool installation]](/img/bc/9b39651d40a240f0893200793f67e9.png)
[RT thread env tool installation]

Some important knowledge of MySQL

Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses

LeetCode_ 7_ five

Openeuler prize catching activities, to participate in?

vulnhub之school 1

Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决

CSDN syntax description

9 原子操作类之18罗汉增强
随机推荐
Ucloud is a basic cloud computing service provider
Visual Studio 插件之CodeMaid自动整理代码
Boot 和 Cloud 的版本选型
8 CAS
Introduction to bit operation
# 欢迎使用Markdown编辑器
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
R language dplyr package select function, group_ The by function, filter function and do function obtain the third largest value of a specific numerical data column in a specified level in a specified
强化学习-学习笔记8 | Q-learning
LeetCode_ 7_ five
Kubernetes——kubectl命令行工具用法详解
Sword finger offer II 013 Sum of two-dimensional submatrix
how to prove compiler‘s correctness
微信公众号OAuth2.0授权登录并显示用户信息
YoloV6:YoloV6+Win10---训练自己得数据集
IP 工具类
Compiler optimization (4): inductive variables
CSDN syntax description
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
LC:字符串转换整数 (atoi) + 外观数列 + 最长公共前缀