当前位置:网站首页>力扣 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
边栏推荐
- 8 CAS
- JVM 类加载机制
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
- 注解。。。
- My creation anniversary
- Openeuler prize catching activities, to participate in?
- [RT thread env tool installation]
- PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
- 841. String hash
- vulnhub之tre1
猜你喜欢
关于ssh登录时卡顿30s左右的问题调试处理
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
Kirin Xin'an cloud platform is newly upgraded!
模拟实现string类
Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
Matplotlib drawing 3D graphics
Welcome to the markdown editor
使用高斯Redis实现二级索引
转置卷积理论解释(输入输出大小分析)
随机推荐
mysql 的一些重要知识
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
ASP. Net gymnasium integrated member management system source code, free sharing
Automatic classification of defective photovoltaic module cells in electroluminescence images-論文閱讀筆記
【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
vulnhub之Funfox2
ASP. Net kindergarten chain management system source code
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
841. 字符串哈希
The project manager's "eight interview questions" is equal to a meeting
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
Cloud 组件发展升级
微信公众号OAuth2.0授权登录并显示用户信息
torch. nn. functional. Pad (input, pad, mode= 'constant', value=none) record
The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
Ways to improve the utilization of openeuler resources 01: Introduction
UCloud是基础云计算服务提供商