当前位置:网站首页>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;
}
边栏推荐
- Babbitt | yuan universe daily must read: Guangzhou Nansha released the "Yuan universe nine" measures, and the platform can obtain up to 200million yuan of financial support
- Explanation of common basic controls for C # form application (suitable for Mengxin)
- 【微服务~Sentinel】Sentinel之dashboard控制面板
- Size limit display of pictures
- [leetcode -- the second day of introduction to programming ability] operator (the number of bit 1 / the difference between the sum of the products of integers)
- Ctfshow ultimate assessment
- async/await的执行顺序以及宏任务和微任务
- [micro service ~sentinel] sentinel dashboard control panel
- 5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises
- Programming style
猜你喜欢

CUDA programming-02: first knowledge of CUDA Programming

Specific methods and steps for Rockwell AB PLC to establish communication with PLC through rslinx classic

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

【ACL2020】一种新颖的成分句法树序列化方法

ES6 new - Operator extension

苹果降价600元,对本就溃败的国产旗舰手机几乎是毁灭性打击

Restful
![[C language _ review _ learn Lesson 2] what is base system? How to convert between hexadecimals](/img/ef/b7a9214e69150c069e352af758f614.png)
[C language _ review _ learn Lesson 2] what is base system? How to convert between hexadecimals
![[interprocess communication IPC] - semaphore learning](/img/47/b76c329e748726097219abce28fce8.png)
[interprocess communication IPC] - semaphore learning

JS call and apply
随机推荐
Activation functions commonly used in deep learning
Understand various IOU loss functions in target detection
PyTorch自定义CUDA算子教程与运行时间分析
拍卖行做VC,第一次出手就投了个Web3
Common operations of BOM and compatible writing methods for obtaining page / window height, width and scrolling
Analog library function
Mangodb简单使用
Qdoublevalidator does not take effect solution
08_ Service fusing hystrix
【微服务~Sentinel】Sentinel之dashboard控制面板
Ctfshow ultimate assessment
ArkUI开发框架组件的生命周期
Nutrecipes developed based on arkui ETS
flex:1的原理
基于restful页面数据交互
[leetcode -- the second day of introduction to programming ability] operator (the number of bit 1 / the difference between the sum of the products of integers)
Pymongo fuzzy query
07_ Service registration and discovery summary
[C language - zero foundation lesson 8] circular structure and break continue
STL container -- Application of set set