当前位置:网站首页>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;
}
边栏推荐
- Linux Installation and remote connection MySQL records
- [C language _ study _ exam _ review lesson 3] overview of ASCII code and C language
- ArkUI中的显式动画
- IDL 6S lookup table
- C language exercise 2
- The third day of learning C language
- Special exercises for beginners of C language to learn code for the first time
- flex布局 (实战小米官网)
- Five kinds of 2D attention finishing (non local, criss cross, Se, CBAM, dual attention)
- 基于ArkUI eTS开发的坚果笑话(NutJoke)
猜你喜欢

【微服务~Sentinel】Sentinel之dashboard控制面板

NPM install error forced installation

NCCL (NVIDIA Collective Communications Library)

Mangodb simple to use

Restful

js call和apply

Linux Installation and remote connection MySQL records

ES6 new - array part

Is the operation of assigning values to int variables atomic?

pollFirst(),pollLast(),peekFirst(),peekLast()
随机推荐
[C language - zero foundation lesson 11] rotate the pointer of the big turntable
Wechat applet 5 - foundation strengthening (not finished)
Huawei machine test question: Martian computing JS
[C language - zero foundation lesson 8] circular structure and break continue
巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
ES6 new - string part
openharmony萌新贡献指南
npm和yarn 更新依赖包
ES6 new - deconstruction assignment of array / object
【微信小程序】农历公历互相转换
[micro service ~sentinel] sentinel dashboard control panel
【微服务~Sentinel】Sentinel之dashboard控制面板
< script> detailed explanation of label content
STL container -- Application of set set
You haven't heard of such 6 question brushing websites, have you? Are you out?
Antdesign a-modal user-defined instruction realizes dragging and zooming in and out
The fourth day of learning C language
对 int 变量赋值的操作是原子的吗?
[C language - zero foundation lesson 7] sequential structure and selection structure
[C language - zero foundation lesson 10] adventure of array Kingdom