当前位置:网站首页>Interview question 01.02. determine whether it is character rearrangement
Interview question 01.02. determine whether it is character rearrangement
2022-07-24 16:37:00 【My_ Bells】
Title Description
Given two strings s1 and s2, Please write a program , After determining the character rearrangement of one of the strings , Can it be another string .
https://leetcode-cn.com/problems/check-permutation-lcci/
Their thinking
There are three ways to achieve :
- Turn it into an array and sort and compare strings
- Compare value results with object count
- Convert the string to ASCII Code full plus comparison value
Code implementation
/** * @param {string} s1 * @param {string} s2 * @return {boolean} */
// Three ways to achieve
//1. Turn it into an array and sort and compare strings // 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. Use object counting //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. Convert the string to ASCII Code full plus comparison value 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;
};
边栏推荐
- OpenMP入门
- Educational codeforces round 100 (rated for Div. 2) B. find the array solution
- 剑指 Offer 22. 链表中倒数第k个节点
- Qt设计仿真机器人控制器
- Intel plans to sell baseband chip business, but Apple has given up?
- thinkphp3.2.5无法跳转到外部链接
- 【技术】uniapp之聊天室 demo
- 随笔记:同步、异步和微任务、宏任务的打印顺序
- Best practices for preventing XSS in PHP web applications
- Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer
猜你喜欢

Duplicate content in lookup table

15、ARM嵌入式系统:如何用PC调试单板

会议OA项目进度(一)

MODIS data WGet Download

Why should we launch getaverse?

【南京农业大学】考研初试复试资料分享

TCP protocol debugging tool tcpengine v1.3.0 tutorial

Thinkphp3.2.5 cannot jump to external links

Telephone system rules
[email protected]"/>ZBar project introduction and installation configuration| [email protected]
随机推荐
OpenMP入门
Meeting OA project progress (II)
[technology] online seat selection demo of uniapp
MySQL converts strings to numeric types and sorts them
ArcGIS pixel size changed from 0.00025 to meters
leetcode:162. 寻找峰值【二分寻找峰值】
Software recommendation - Installation
Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
How to prevent XSS in PHP
Wechat applet list (list rendering of data rendering)
Why should we launch getaverse?
EF data migration
Qualcomm reconciled with apple and received at least $4.5 billion in settlement fees! Next goal: Huawei!
Software recommendation - Mechanical Major
Qt设计机器人仿真控制器——按键控制机器人关节转动
Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
Microsoft Visio professional 2013 cannot be opened or uninstalled. Solution
Envi SHP to ROI and mask the grid
How to effectively avoid memory leakage when customizing the handler?
The 3D sensing market is accelerating. Who will be better, TOF or structured light?