当前位置:网站首页>力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
2022-07-01 07:46:00 【重邮研究森】
2022.6.30今天你刷题了吗?
题目:
给你长度相等的两个字符串 s1 和 s2 。一次 字符串交换 操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。
如果对 其中一个字符串 执行 最多一次字符串交换 就可以使两个字符串相等,返回 true ;否则,返回 false 。
分析:
对于看两个字符串,通过交换一次字符串,能不能使得两个字符串相等。
思路:遍历其中一个字符串,当下标相同但元素不一样时,把这两个不同的元素分别保存,然后继续遍历,如果又出现不同,则判断此时如果利用之前保存元素和现在需要交换的是不是一样,一样则是可以保存,不一样则不行,并且直接退出。如果一样就行继续判断后续是否还需要交换
解析:
1.暴力求解
class Solution {
public:
bool areAlmostEqual(string s1, string s2) {
char c1='#',c2=c1;
bool isChange=false;
int n=s1.size();
for(int i=0;i<n;i++){
if(s1[i]!=s2[i]){
if(isChange)return false;
else if(c1=='#'){
c1=s1[i];
c2=s2[i];
}
else{
if(c1==s2[i]&&c2==s1[i]){
isChange=true;
c2='#';
}
else return false;
}
}
}
//看c2是否为#
return c2=='#';
}
};边栏推荐
- redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
- [programming training 2] sorting subsequence + inverted string
- C language implementation [minesweeping game] full version (implementation source code)
- [skill] create Bat quick open web page
- Mysql与Redis一致性解决方案
- C language implementation [Sanzi chess game] (step analysis and implementation source code)
- Software testing methods and techniques - overview of basic knowledge
- Redisson uses the full solution - redisson official documents + comments (Part 2)
- Todolist classic case ①
- 关系数据库如何工作
猜你喜欢

TodoList经典案例①

ctfshow-web355,356(SSRF)

2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation

JAX的深度学习和科学计算

038 network security JS

2022年茶艺师(中级)复训题库及答案

1286_ Implementation analysis of task priority setting in FreeRTOS

Atguigu---- scaffold --02- use scaffold (2)

Thesis learning -- Analysis and Research on similarity query of hydrological time series
![[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array](/img/5e/068268857b8c7c2065ba552fd23dbb.png)
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
随机推荐
Software testing methods and techniques - overview of basic knowledge
How to create an exclusive vs Code theme
redisson使用全解——redisson官方文档+注释(中篇)
[target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)
The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
matlab保存DB4i深度相机图片
关系数据库如何工作
浅谈CVPR2022的几个研究热点
LeetCode+ 71 - 75
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
Will Internet talents be scarce in the future? Which technology directions are popular?
Todolist classic case ①
[软件] phantomjs屏幕截图
Vhost kick & call principle
Long way to go with technology
[untitled]
Is it safe and reliable for Huatai Securities to open an account? How to open Huatai Securities Account
Apple account password auto fill
Do securities account opening affect the security of account opening
She is the "HR of others" | ones character