当前位置:网站首页>LeetCode-859. Intimate string
LeetCode-859. Intimate string
2022-06-11 16:59:00 【Border wanderer】
Here are two strings s and goal , As long as we can exchange s The two letters in get and goal Equal result , Just go back to true ; Otherwise return to false .
The definition of exchanging letters is : Take two subscripts i and j ( Subscript from 0 Start ) And meet i != j , Then exchange s[i] and s[j] The character at .
for example , stay "abcd" Exchange subscripts in 0 And subscripts 2 Elements of can generate "cbad" .
Example 1:
Input :s = "ab", goal = "ba"
Output :true
explain : You can exchange s[0] = 'a' and s[1] = 'b' Generate "ba", here s and goal equal .
Example 2:
Input :s = "ab", goal = "ab"
Output :false
explain : You can only exchange s[0] = 'a' and s[1] = 'b' Generate "ba", here s and goal It's not equal .
Example 3:
Input :s = "aa", goal = "aa"
Output :true
explain : You can exchange s[0] = 'a' and s[1] = 'a' Generate "aa", here s and goal equal .
Example 4:
Input :s = "aaaaaaabc", goal = "aaaaaaacb"
Output :true
Tips :
1 <= s.length, goal.length <= 2 * 104
s and goal It's made up of lowercase letters
#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;
}边栏推荐
- Database backup (MySQL)
- unittest 如何知道每个测试用例的执行时间
- How to store tree structure in database
- 啟牛商學院給的證券賬戶是安全的嗎?開戶收費嗎
- Rdkit tutorial
- ShellBrowser . NET Crack
- Leetcode 450. Deleting a node in a binary search tree
- Weekly recommended short video: rookie CEO talks about the new logistics track in the future
- Regression prediction | realization of RBF RBF neural network with multiple inputs and single output by MATLAB
- Research Report on operation mode and investment opportunities of China's aluminum industry 2022-2028
猜你喜欢

WYSIWYG markdown editor: typora

Science popularization genius on the left, madman on the right

开关电源电路图及原理12v分析-详细版
![[opencvsharp] spot detection barcode decoding image operation image rotation / flip / Zoom perspective transformation image display control demo notes](/img/a7/5b66ef0360ed41441653a413cd9e53.png)
[opencvsharp] spot detection barcode decoding image operation image rotation / flip / Zoom perspective transformation image display control demo notes

How unittest knows the execution time of each test case

基于文本驱动用于创建和编辑图像(附源代码)

LeetCode——42. 接雨水(双指针)

485 days, 21 experiences of my remote office sharing | community essay solicitation

Research Report on operation mode and investment opportunities of China's aluminum industry 2022-2028

ShellBrowser . NET Crack
随机推荐
ShellBrowser .NET Crack
核密度估计(二维、三维)
Weekly recommended short video: rookie CEO talks about the new logistics track in the future
ASP. Net education OA system source code education industry OA system source code with document
Global and China Mobile Network Optimization (MnO) industry development panoramic survey and Investment Strategy Research Report 2022-2028
Elasitcsearch基础学习笔记(1)
Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
笔记本连接外部键盘后,如何把笔记本自身的键盘禁用
Center detection of circular marking points for opencv camera calibration
Using MATLAB and dcraw to process digital camera raw files
【opencvsharp】opencvsharp_samples.core示例代码笔记
Difference between select into from and insert into select
从0到1了解Prometheus
Oracle database merge row records, wmsys WM_ Use of the concat function and group in MySQL_ Use and comparison of concat (ID).
开关电源电路图及原理12v分析-详细版
Time series prediction | MATLAB realizes future multi-step prediction of RBF RBF neural network time series
【pytest学习】pytest 用例执行失败后其他不再执行
Oracle analysis function over and MySQL achieve similar effects
多任务学习经典品读:MMoE模型篇
cocoapod只更新指定库(不更新索引)