当前位置:网站首页>2068. Check whether the two strings are almost equal
2068. Check whether the two strings are almost equal
2022-07-27 09:22:00 【Mr Gao】
2068. Check that the two strings are almost equal
If two strings word1 and word2 In the from ‘a’ To ‘z’ The difference in the frequency of each letter is No more than 3 , So we call these two strings word1 and word2 Almost equal .
Here you are. Both lengths are n String word1 and word2 , If word1 and word2 Almost equal , Please return true , Otherwise return to false .
A letter x Appearance frequency It refers to the number of times it appears in the string .
Example 1:
Input :word1 = “aaaa”, word2 = “bccb”
Output :false
explain : character string “aaaa” There is 4 individual ‘a’ , however “bccb” There is 0 individual ‘a’ .
The difference between the two is 4 , Greater than upper limit 3 .
Example 2:
Input :word1 = “abcdeef”, word2 = “abaaacc”
Output :true
explain :word1 and word2 The difference in the frequency of each letter in the is at most 3 :
- ‘a’ stay word1 In the 1 Time , stay word2 In the 4 Time , The difference is 3 .
- ‘b’ stay word1 In the 1 Time , stay word2 In the 1 Time , The difference is 0 .
- ‘c’ stay word1 In the 1 Time , stay word2 In the 2 Time , The difference is 1 .
- ‘d’ stay word1 In the 1 Time , stay word2 In the 0 Time , The difference is 1 .
- ‘e’ stay word1 In the 2 Time , stay word2 In the 0 Time , The difference is 2 .
- ‘f’ stay word1 In the 1 Time , stay word2 In the 0 Time , The difference is 1 .
Example 3:
Input :word1 = “cccddabba”, word2 = “babababab”
Output :true
explain :word1 and word2 The difference in the frequency of each letter in the is at most 3 :
- ‘a’ stay word1 In the 2 Time , stay word2 In the 4 Time , The difference is 2 .
- ‘b’ stay word1 In the 2 Time , stay word2 In the 5 Time , The difference is 3 .
- ‘c’ stay word1 In the 3 Time , stay word2 In the 0 Time , The difference is 3 .
- ‘d’ stay word1 In the 2 Time , stay word2 In the 0 Time , The difference is 2 .
This question is still very interesting , The solution code is as follows :
bool checkAlmostEquivalent(char * word1, char * word2){
int r[26];
int i;
for(i=0;i<26;i++){
r[i]=0;
}
for(i=0;word1[i]!='\0';i++){
r[word1[i]-'a']++;
}
for(i=0;word2[i]!='\0';i++){
r[word2[i]-'a']--;
}
for(i=0;i<26;i++){
if(abs(r[i])>3){
return false;
}
}
return true;
}
边栏推荐
- JS call and apply
- You haven't heard of such 6 question brushing websites, have you? Are you out?
- Restful
- < script> detailed explanation of label content
- Thermal renewal and its principle
- Flex layout (actual Xiaomi official website)
- A survey of robust lidar based 3D object detection methods for autonomous driving paper notes
- Antdesign a-modal自定义指令实现拖拽放大缩小
- 坚果天气
- [C language _ review _ learn Lesson 2] what is base system? How to convert between hexadecimals
猜你喜欢

Analog library function
![[leetcode -- the first day of introduction to programming ability] basic data type [statistics of odd numbers within the range / average wage after removing the minimum wage and maximum wage)](/img/23/497c013d105d1906ae8a37dd4e18ad.png)
[leetcode -- the first day of introduction to programming ability] basic data type [statistics of odd numbers within the range / average wage after removing the minimum wage and maximum wage)

拍卖行做VC,第一次出手就投了个Web3
![[acl2020] a novel method of component syntax tree serialization](/img/24/b8ec489966f7b1deef82b2eefa4d1b.png)
[acl2020] a novel method of component syntax tree serialization

【无标题】

Wechat applet 5 - foundation strengthening (not finished)

You haven't heard of such 6 question brushing websites, have you? Are you out?

Pytorch custom CUDA operator tutorial and runtime analysis

NPM install error forced installation

Data interaction based on restful pages
随机推荐
You haven't heard of such 6 question brushing websites, have you? Are you out?
Apple cut its price by 600 yuan, which was almost a devastating blow to the collapse of its domestic flagship mobile phone
Antdesign a-modal自定义指令实现拖拽放大缩小
音乐体验天花板!14个网易云音乐的情感化设计细节
Linux Installation and remote connection MySQL records
【ACL2020】一种新颖的成分句法树序列化方法
IDL reads HSD data of sunflower 8 (himawari-8)
How to optimize the deep learning model to improve the reasoning speed
Data interaction based on restful pages
基于ArkUI eTS开发的坚果食谱(NutRecipes
Full Permutation recursive thought sorting
Mangodb简单使用
The second day of learning C language
Two tips in arkui framework
Day 8 of learning C language
The difference between computed and watch
[daily algorithm day 96] Tencent interview question: merge two ordered arrays
ES6 new - function part
拍卖行做VC,第一次出手就投了个Web3
IDL 6S lookup table