当前位置:网站首页>面试题 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;
}
边栏推荐
- The unity vector rotates at a point
- Regular expression string
- A link opens the applet code. After compilation, it is easy to understand
- Shader_ Animation sequence frame
- What are compiled languages and interpreted languages?
- 应用程序和matlab的通信方式
- 【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
- Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
- 如何在shell中实现 backspace
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
猜你喜欢

Enterprise log analysis system elk

【Vulnhub靶场】THALES:1

2022第四届中国(济南)国际智慧养老产业展览会,山东老博会

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

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

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

Numpy -- epidemic data analysis case
Notification uses full resolution

Unity3D_ Class fishing project, bullet rebound effect is achieved

Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
随机推荐
Unity3d click events added to 3D objects in the scene
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
A JS script can be directly put into the browser to perform operations
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
Laravel5.1 路由 -路由分组
【Android -- 数据存储】使用 SQLite 存储数据
logback.xml配置不同级别日志,设置彩色输出
01tire+链式前向星+dfs+贪心练习题.1
torch. Numel action
Odoo集成Plausible埋码监控平台
企业级日志分析系统ELK
ThinkPHP URL 路由简介
How can laravel get the public path
【Vulnhub靶场】THALES:1
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
[excelexport], Excel to Lua, JSON, XML development tool
Step by step monitoring platform ZABBIX
laravel构造函数和中间件执行顺序问题