当前位置:网站首页>面试题 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;
}
边栏推荐
猜你喜欢
HAVE FUN | “飞船计划”活动最新进展
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
torch.numel作用
Talk about the cloud deployment of local projects created by SAP IRPA studio
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
Unity3d click events added to 3D objects in the scene
预测——灰色预测
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
分步式監控平臺zabbix
Statistical learning method -- perceptron
随机推荐
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
记一次项目的迁移过程
What is the difference between IP address and physical address
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
修改配置文件后tidb无法启动
Numpy -- epidemic data analysis case
TCP framework___ Unity
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
markdown公式编辑教程
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
Leetcode-136- number that appears only once (solve with XOR)
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
MySQL数据库基本操作-DQL-基本查询
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
121. 买卖股票的最佳时机
Vs tool word highlight with margin