当前位置:网站首页>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=='#';
}
};边栏推荐
- 力扣——求一组字符中的第一个回文字符
- Download xshell and xftp
- 【无标题】
- Missing API interface actual development series (14): ID card real name authentication verification
- Jax's deep learning and scientific computing
- The triode is a great invention
- [MySQL learning notes27] stored procedure
- Minecraft 1.16.5 module development (51) tile entity
- Redisson uses the full solution - redisson official documents + comments (Part 2)
- Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
猜你喜欢

Gru of RNN

Download xshell and 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](/img/8d/cf259b9bb8574aa1842280c9661d1e.jpg)
[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

【mysql学习笔记25】sql语句优化

Ctfhub port scan (SSRF)

【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)

iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事

三极管是一项伟大的发明

The database is locked. Is there a solution
随机推荐
Félicitations pour l'inscription réussie de wuxinghe
Kickback -- find the first palindrome character in a group of characters
[软件] phantomjs屏幕截图
2022 electrician (intermediate) recurrent training question bank and answers
组件的自定义事件②
atguigu----脚手架--02-使用脚手架(2)
redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
[programming training] delete public characters (hash mapping) + team competition (greedy)
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
Oracle创建自增id
Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
[skill] create Bat quick open web page
长路漫漫、技术作伴
redisson使用全解——redisson官方文檔+注釋(上篇)
base64
Warm congratulations on the successful listing of five elements hehe liquor
Redisson uses the full solution - redisson official document + comments (Part 2)
C # read and write customized config file
2022危险化学品经营单位主要负责人试题及模拟考试
C# Newtonsoft.Json中JObject的使用