当前位置:网站首页>2068. 检查两个字符串是否几乎相等
2068. 检查两个字符串是否几乎相等
2022-07-27 09:14:00 【Mr Gao】
2068. 检查两个字符串是否几乎相等
如果两个字符串 word1 和 word2 中从 ‘a’ 到 ‘z’ 每一个字母出现频率之差都 不超过 3 ,那么我们称这两个字符串 word1 和 word2 几乎相等 。
给你两个长度都为 n 的字符串 word1 和 word2 ,如果 word1 和 word2 几乎相等 ,请你返回 true ,否则返回 false 。
一个字母 x 的出现 频率 指的是它在字符串中出现的次数。
示例 1:
输入:word1 = “aaaa”, word2 = “bccb”
输出:false
解释:字符串 “aaaa” 中有 4 个 ‘a’ ,但是 “bccb” 中有 0 个 ‘a’ 。
两者之差为 4 ,大于上限 3 。
示例 2:
输入:word1 = “abcdeef”, word2 = “abaaacc”
输出:true
解释:word1 和 word2 中每个字母出现频率之差至多为 3 :
- ‘a’ 在 word1 中出现了 1 次,在 word2 中出现了 4 次,差为 3 。
- ‘b’ 在 word1 中出现了 1 次,在 word2 中出现了 1 次,差为 0 。
- ‘c’ 在 word1 中出现了 1 次,在 word2 中出现了 2 次,差为 1 。
- ‘d’ 在 word1 中出现了 1 次,在 word2 中出现了 0 次,差为 1 。
- ‘e’ 在 word1 中出现了 2 次,在 word2 中出现了 0 次,差为 2 。
- ‘f’ 在 word1 中出现了 1 次,在 word2 中出现了 0 次,差为 1 。
示例 3:
输入:word1 = “cccddabba”, word2 = “babababab”
输出:true
解释:word1 和 word2 中每个字母出现频率之差至多为 3 :
- ‘a’ 在 word1 中出现了 2 次,在 word2 中出现了 4 次,差为 2 。
- ‘b’ 在 word1 中出现了 2 次,在 word2 中出现了 5 次,差为 3 。
- ‘c’ 在 word1 中出现了 3 次,在 word2 中出现了 0 次,差为 3 。
- ‘d’ 在 word1 中出现了 2 次,在 word2 中出现了 0 次,差为 2 。
这题还是挺有趣的,解题代码如下:
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;
}
边栏推荐
- 存储和计算引擎
- Mangodb简单使用
- Explanation of common basic controls for C # form application (suitable for Mengxin)
- Summary of traversal methods
- js call和apply
- ES6 new - string part
- [C language - zero foundation lesson 8] circular structure and break continue
- flex布局 (实战小米官网)
- What if the parameters in QT are structs or custom classes when sending signals?
- How to deploy yolov6 with tensorrt
猜你喜欢

音乐体验天花板!14个网易云音乐的情感化设计细节

CUDA programming-02: first knowledge of CUDA Programming

Music experience ceiling! Emotional design details of 14 Netease cloud music

一些实用、常用、效率越来越高的 Kubernetes 别名

Some practical, commonly used and increasingly efficient kubernetes aliases

Understand various IOU loss functions in target detection

JS call and apply
![[C language - zero foundation lesson 6] input and output sentence format and compound sentence](/img/d5/b9935a37b634db7f5740779009e44f.png)
[C language - zero foundation lesson 6] input and output sentence format and compound sentence

B tree

How to optimize the deep learning model to improve the reasoning speed
随机推荐
[interprocess communication IPC] - semaphore learning
500 error reporting
Restful
MySQL basic knowledge learning (I)
Deep understanding of Kalman filter (1): background knowledge
Five kinds of 2D attention finishing (non local, criss cross, Se, CBAM, dual attention)
[C language - zero foundation lesson 13] the mystery of string
Nut joke based on arkui ETS
Flex layout (actual Xiaomi official website)
[C language - zero foundation _ study _ review _ lesson 4] data types and operations
SQL exercise set
坚果天气
Software testing function testing a full set of common interview questions [function testing - zero foundation] essential 4-1
Pymongo fuzzy query
Tensorflow loss function
Restful
微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
QDoubleValidator不生效问题解决办法
How to register code cloud account
Qdoublevalidator does not take effect solution