当前位置:网站首页>面试题 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;
}
边栏推荐
- [excelexport], Excel to Lua, JSON, XML development tool
- Three. JS introductory learning notes 19: how to import FBX static model
- 【C 语言】 题集 of Ⅹ
- Mysql database backup script
- thinkphp3.2.3中设置路由,优化url
- 分步式監控平臺zabbix
- 121. The best time to buy and sell stocks
- JS modularization
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- pycharm 终端部启用虚拟环境
猜你喜欢

统计学习方法——感知机

SPI master rx time out中断

星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”

TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
Notification uses full resolution

Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench

The unity vector rotates at a point

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

删除 console 语句引发的惨案

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
随机推荐
Asyncio concept and usage
asyncio 概念和用法
Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
121. The best time to buy and sell stocks
Odoo集成Plausible埋码监控平台
markdown公式编辑教程
Limit of total fields [1000] in index has been exceeded
Sysom case analysis: where is the missing memory| Dragon lizard Technology
You Yuxi, coming!
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
torch.numel作用
TCP framework___ Unity
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
How does geojson data merge the boundaries of regions?
SPI master rx time out中断
Description of vs common shortcut keys
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
Logback日志框架第三方jar包 免费获取
Regular expression string
Balanced binary tree (AVL)