当前位置:网站首页>面试题 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;
}
边栏推荐
- Plate - forme de surveillance par étapes zabbix
- 用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
- 目标跟踪常见训练数据集格式
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- MySQL中, 如何查询某一天, 某一月, 某一年的数据
- Enterprise log analysis system elk
- markdown公式编辑教程
- 平衡二叉树(AVL)
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
猜你喜欢

HAVE FUN | “飞船计划”活动最新进展

Enterprise log analysis system elk

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

Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?

Wireless sensor networks -- ZigBee and 6LoWPAN

Odoo integrated plausible embedded code monitoring platform

Leetcode-231-2的幂

Dotween -- ease function

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

Numpy -- data cleaning
随机推荐
A JS script can be directly put into the browser to perform operations
IP地址和物理地址有什么区别
Asyncio concept and usage
47_Opencv中的轮廓查找 cv::findContours()
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
01tire+链式前向星+dfs+贪心练习题.1
PHP实现执行定时任务的几种思路详解
TCP framework___ Unity
Vs tool word highlight with margin
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
企业级日志分析系统ELK
laravel中将session由文件保存改为数据库保存
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
Tragedy caused by deleting the console statement
Three singleton modes of unity (hungry man, lazy man, monobehavior)
Introduction to pyGame games
Logback logging framework third-party jar package is available for free
asyncio 概念和用法
[excelexport], Excel to Lua, JSON, XML development tool
Common training data set formats for target tracking