当前位置:网站首页>面试题 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;
}
边栏推荐
- PHP实现执行定时任务的几种思路详解
- laravel怎么获取到public路径
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- U3D_ Infinite Bessel curve
- SPI master rx time out中断
- Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
- 无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
- 华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
- 修改配置文件后tidb无法启动
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
猜你喜欢

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

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

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

How does geojson data merge the boundaries of regions?

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

Balanced binary tree (AVL)

Wireless sensor networks -- ZigBee and 6LoWPAN

spark调优(三):持久化减少二次查询

Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
随机推荐
47_ Contour lookup in opencv cv:: findcontours()
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
Wireless sensor networks -- ZigBee and 6LoWPAN
Three singleton modes of unity (hungry man, lazy man, monobehavior)
记一次项目的迁移过程
TCP framework___ Unity
Unity3D_ Class fishing project, bullet rebound effect is achieved
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
Migration and reprint
AE learning 01: AE complete project summary
Shader_ Animation sequence frame
Markdown formula editing tutorial
121. The best time to buy and sell stocks
Mysql database backup script
What is the difference between IP address and physical address
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
Plate - forme de surveillance par étapes zabbix
laravel构造函数和中间件执行顺序问题