当前位置:网站首页>面试题 01.02. 判定是否互为字符重排-辅助数组算法
面试题 01.02. 判定是否互为字符重排-辅助数组算法
2022-07-07 14:15:00 【Mr Gao】
面试题 01.02. 判定是否互为字符重排
给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入: s1 = “abc”, s2 = “bca”
输出: true
示例 2:
输入: s1 = “abc”, s2 = “bad”
输出: false
对于这一题,其实比较简单,我们添加一个辅助数组去判断问题就可以了,做一个辅助数组,存储s1中各个字母的数量 ,然后,对s2中存储的字母数量进行相减,辅助数组最后存储各个字母数量都为0,返回true,否则返回false
解题代码如下:
bool CheckPermutation(char* s1, char* s2){
int r[26];
int i=0;
for(i=0;i<26;i++){
r[i]=0;
}
i=0;
while(s1[i]!='\0'){
r[s1[i]-'a']++;
i++;
}
i=0;
while(s2[i]!='\0'){
r[s2[i]-'a']--;
i++;
}
for(i=0;i<26;i++){
if(r[i]!=0){
return false;
}
}
return true;
}
边栏推荐
- JS 模块化
- Vs tool word highlight with margin
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- Introduction to pyGame games
- 企业级日志分析系统ELK
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- Tragedy caused by deleting the console statement
- Numpy -- data cleaning
- Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
- Power of leetcode-231-2
猜你喜欢

分步式监控平台zabbix

Unity3d click events added to 3D objects in the scene

TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比

统计学习方法——感知机
通知Notification使用全解析

Logback日志框架第三方jar包 免费获取

Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)

Tragedy caused by deleting the console statement

Dotween -- ease function
随机推荐
Dotween -- ease function
laravel中将session由文件保存改为数据库保存
How does geojson data merge the boundaries of regions?
Logback logging framework third-party jar package is available for free
Migration and reprint
PHP realizes wechat applet face recognition and face brushing login function
JS modularization
How to determine whether the checkbox in JS is selected
logback.xml配置不同级别日志,设置彩色输出
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
iptables只允许指定ip地址访问指定端口
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
hellogolang
分类模型评价标准(performance measure)
MySQL中, 如何查询某一天, 某一月, 某一年的数据
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Power of leetcode-231-2
You Yuxi, coming!