当前位置:网站首页>Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
2022-07-01 07:48:00 【Chongyou research Sen】
2022.6.30 Did you brush the questions today ?
subject :
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 .
analysis :
For looking at two strings , By exchanging strings once , Can you make two strings equal .
Ideas : Traversing one of the strings , When the subscripts are the same but the elements are different , Save these two different elements separately , And then continue to traverse , If something different happens , Then judge whether the elements saved before are the same as those to be exchanged now , The same can be saved , Not the same , And exit directly . If it's the same, continue to judge whether it needs to be exchanged in the future
analysis :
1. Violent solution
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;
}
}
}
// see c2 Is it #
return c2=='#';
}
};边栏推荐
- Array: question brushing record
- 组件的自定义事件②
- Autosar 学习记录(1) – EcuM_Init
- [MySQL learning notes 25] SQL statement optimization
- Huawei modelarts training alexnet model
- [Shenzhen IO] precise Food Scale (some understanding of assembly language)
- 下载Xshell和Xftp
- [chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
- 2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
- [MySQL learning notes27] stored procedure
猜你喜欢

【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)

2022危险化学品经营单位主要负责人试题及模拟考试

浅谈CVPR2022的几个研究热点

ctfshow-web352,353(SSRF)

Vhost kick & call principle

2022年流动式起重机司机考试练习题及在线模拟考试

Array: question brushing record

微软宣布开源 (GODEL) 语言模型聊天机器人

Download xshell and xftp

Will Internet talents be scarce in the future? Which technology directions are popular?
随机推荐
weback5基础配置详解
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
长路漫漫、技术作伴
2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
[MySQL learning notes 25] SQL statement optimization
atguigu----脚手架--02-使用脚手架(2)
浏览器本地存储
[MySQL learning notes27] stored procedure
【mysql学习笔记28】存储函数
Redisson uses the full solution - redisson official documents + comments (Part 2)
熱烈祝賀五行和合酒成功掛牌
组件的自定义事件②
Basic number theory -- combinatorial number
kubernetes资源对象介绍及常用命令(二)
[microservice openfeign] feign's log record
【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
Detailed explanation of weback5 basic configuration
[untitled]
【R语言】两个/N个数据合并merge函数
Custom events of components ②