当前位置:网站首页>LeetCode-859. 亲密字符串
LeetCode-859. 亲密字符串
2022-06-11 16:53:00 【边界流浪者】
给你两个字符串 s 和 goal ,只要我们可以通过交换 s 中的两个字母得到与 goal 相等的结果,就返回 true ;否则返回 false 。
交换字母的定义是:取两个下标 i 和 j (下标从 0 开始)且满足 i != j ,接着交换 s[i] 和 s[j] 处的字符。
例如,在 "abcd" 中交换下标 0 和下标 2 的元素可以生成 "cbad" 。
示例 1:
输入:s = "ab", goal = "ba"
输出:true
解释:你可以交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 相等。
示例 2:
输入:s = "ab", goal = "ab"
输出:false
解释:你只能交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 不相等。
示例 3:
输入:s = "aa", goal = "aa"
输出:true
解释:你可以交换 s[0] = 'a' 和 s[1] = 'a' 生成 "aa",此时 s 和 goal 相等。
示例 4:
输入:s = "aaaaaaabc", goal = "aaaaaaacb"
输出:true
提示:
1 <= s.length, goal.length <= 2 * 104
s 和 goal 由小写英文字母组成
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
bool buddyStrings(string s, string goal) {
int count = 0;
unordered_map<char, int> hashMp1;
unordered_map<char, int> hashMp2;
bool isBigThanTwo = false;
if (s.length() != goal.length()) {
return false;
}
for (int i = 0; i < s.size(); i++) {
if (hashMp1.count(s[i]) == 0) {
hashMp1[s[i]] = 1;
}
else {
hashMp1[s[i]]++;
}
if (hashMp2.count(goal[i]) == 0) {
hashMp2[goal[i]] = 1;
}
else {
hashMp2[goal[i]]++;
}
if (s[i] != goal[i]) {
count++;
}
}
unordered_map<char, int>::iterator it = hashMp1.begin();
while (it != hashMp1.end()) {
if (hashMp1[it->first] > 1) {
isBigThanTwo = true;
}
if (hashMp1[it->first] != hashMp2[it->first]) {
return false;
}
it++;
}
if (isBigThanTwo && s == goal) {
return true;
}
if (count == 2) {
return true;
}
return false;
}
};
int main() {
Solution* ps = new Solution();
cout << ps->buddyStrings("ab", "ba");
return 0;
}边栏推荐
- Kernel density estimation (2D, 3D)
- Report on the operation situation and future prospects of China's gear oil industry (2022-2028)
- LeetCode——24. 两两交换链表中的节点(三指针)
- CS0006 C# 未能找到元数据文件“C:\Users\...问题
- LeetCode——42. 接雨水(双指针)
- Persistence mechanism of redis
- Katalon Studio Enterprise
- 启牛商学院给的证券账户是安全的吗?开户收费吗
- seed-emulator下进行sql注入攻击(含sql环境配置)
- Simulated 100 questions and simulated examination for main principals of hazardous chemical business units in 2022
猜你喜欢

Regression prediction | realization of RBF RBF neural network with multiple inputs and single output by MATLAB

微服务连接云端Sentinel 控制台失败及连接成功后出现链路空白问题(已解决)

Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)

Why does chip design also need "Craftsmanship"?

Pycharm使用小技巧 - 如何设置背景图片

一套ThinkPHP微信小程序商城源码带后台管理

RSP:遥感预训练的实证研究

基于udp端口猜测的内网穿透

Center detection of circular marking points for opencv camera calibration

Tornado environment construction and basic framework construction -- familiar Hello World
随机推荐
2022年危险化学品经营单位主要负责人考试模拟100题及模拟考试
消息队列-推/拉模式学习 & ActiveMQ及JMS学习
【opencvsharp】opencvsharp_ samples. Core sample code Notes
时序预测 | MATLAB实现RBF径向基神经网络时间序列未来多步预测
Rdkit tutorial
2022年安全员-B证国家题库及模拟考试
Message queue push / pull mode Learning & ActiveMQ and JMS learning
基于udp端口猜测的内网穿透
2022G1工业锅炉司炉考题及模拟考试
Switching power supply circuit diagram and principle 12V analysis - detailed version
Web security - shooting range notes
GemBox.Bundle 43.0 Crack
A journey of database full SQL analysis and audit system performance optimization
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
微服务连接云端Sentinel 控制台失败及连接成功后出现链路空白问题(已解决)
Le compte de titres de l'école de commerce kainiu est - il sécurisé? Frais d'ouverture de compte
How unittest knows the execution time of each test case
Time series prediction | MATLAB realizes future multi-step prediction of RBF RBF neural network time series
一套ThinkPHP微信小程序商城源码带后台管理
Oracle generates non duplicate string sys_ Guid() and MySQL generate unique values