当前位置:网站首页>面试题 01.02. 判定是否互为字符重排
面试题 01.02. 判定是否互为字符重排
2022-07-21 15:00:00 【My_Bells】
题目描述
给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
https://leetcode-cn.com/problems/check-permutation-lcci/
解题思路
有三种方法实现:
- 转为数组然后排序比较字符串
- 利用对象计数比较值结果
- 将字符串转为ASCII码全加比较值
代码实现
/** * @param {string} s1 * @param {string} s2 * @return {boolean} */
//三种方法实现
//1.转为数组然后排序比较字符串// 60 ms 34.1 MB
var CheckPermutation = function(s1, s2) {
let n1=s1.split('').sort().join('');
let n2=s2.split('').sort().join('');
return n1===n2;
};
//2.利用对象计数//60 ms 34 MB
var CheckPermutation = function(s1, s2) {
if(s1.length!==s2.length){
return false;
}
let map={
};
for(let i=0;i<s1.length;i++){
map[s1[i]]=(map[s1[i]]||0)+1;
}
for(let i=0;i<s2.length;i++){
if(!map[s2[i]]){
return false;
}else{
map[s2[i]]--;
}
}
return true;
};
//3.将字符串转为ASCII码全加比较值 64 ms 34 MB
var CheckPermutation = function(s1, s2) {
let sum1=0;
let sum2=0;
if(s1.length!==s2.length)return false;
for(let i=0;i<s1.length;i++){
sum1+=s1.charCodeAt(i);
sum2+=s2.charCodeAt(i);
}
return sum1===sum2;
};
边栏推荐
- 【ICCV 2019】ACNet: 使用非对称卷积块增强CNN的卷积核骨架
- Keithley software 2600 series 2635b | 2636b | 2651a | 2657a ns SourceMeter source table software
- U++ learning notes floating actor
- QT图形界面初学者项目 - 无人机群作战仿真模拟
- 【数学基础】 foundation of mathematics :NPC问题
- 想要制作沙盒游戏?那么这一款插件你一定不能错过(Unity3D)
- 启牛老师让开证券账户安全吗?可以开吗?
- U++ UPROPERTY UFUNCTION 基础
- OSPF experimental demonstration (Huawei router device configuration)
- 吉时利Keithley软件2600系列2601B|2602B|2604B|2606B NS-SourceMeter源表软件
猜你喜欢
Qt之QML虚拟键盘
unity GetAxis(string axisName); How did the axisname value come from
还在被电影中吧爆炸的画面震撼?那你一定不要错过这款Unity的爆炸插件
During the attack and defense drill | know Chuangyu Zhao Wei: let the security ability grow on the cloud
rider 切换主题
抽象类与接口
课题六 - 消息队列实现客户机通信
Gisley keithley Software 2600 Series 2635b | 2636b | 2651a | 2657a NS sourcemeter source table Software
cpu比较
idea 改变字体大小
随机推荐
Fluent future asynchronous processing
吉时利Keithley软件2600系列2601B|2602B|2604B|2606B NS-SourceMeter源表软件
【数学基础】 foundation of mathematics :拉格朗日优化和对偶
剑指 Offer 17. 打印从1到最大的n位数(大数问题)
Keithley software 2400 | 2440 | 2450 | 2460 | 2461 | 2470 ns SourceMeter source table software
unity 3D 修改默认编辑器
异常处理丨一个小案例,带你解决NullPointerException
【ICCV 2019】ACNet: 使用非对称卷积块增强CNN的卷积核骨架
Shortcut keys for switching between the latest version of win10 and changing languages
jetbrans rider 格式化代码时,{}不换行
Detailed explanation of PLC cascade PID control (temperature control of furnace and central air conditioning system)
unity 设置物体不能被穿透
swagger接口导入Postman
运行时,物体移动旋转缩放插件,“RuntimeTransformGizmos插件”使用教程(Unity3D)
Leetcode 1217 playing chips [greedy] the leetcode road of heroding
In the classic mode of dynamics CRM 365 OP version, the tab is scrolled. Can it be displayed through the tab like the UCI interface
UE4 学习笔记 CheckBox替代button + image
How to unlock HTC mobile phones officially
unity 3中获取指定gameObject的方式
When jetbrans rider formats code, {} does not wrap