当前位置:网站首页>面试题 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;
}
边栏推荐
- Use moviepy Editor clips videos and intercepts video clips in batches
- laravel构造函数和中间件执行顺序问题
- Limit of total fields [1000] in index has been exceeded
- Enterprise log analysis system elk
- JS 模块化
- Communication mode between application program and MATLAB
- Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
- Three. JS introductory learning notes 19: how to import FBX static model
- 目标跟踪常见训练数据集格式
- 企业级日志分析系统ELK
猜你喜欢
Unity3d click events added to 3D objects in the scene
Plate - forme de surveillance par étapes zabbix
统计学习方法——感知机
Dotween -- ease function
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Unity drawing plug-in = = [support the update of the original atlas]
AE learning 01: AE complete project summary
平衡二叉树(AVL)
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
Enterprise log analysis system elk
随机推荐
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
AE learning 02: timeline
121. 买卖股票的最佳时机
PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
You Yuxi, coming!
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Asyncio concept and usage
修改配置文件后tidb无法启动
How to query the data of a certain day, a certain month, and a certain year in MySQL
How to implement backspace in shell
TCP framework___ Unity
AE learning 01: AE complete project summary
招标公告:盘锦市人民医院盘锦医院数据库维保项目
Migration and reprint
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
模仿企业微信会议室选择
MySQL中, 如何查询某一天, 某一月, 某一年的数据
PHP realizes wechat applet face recognition and face brushing login function