当前位置:网站首页>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;
};
边栏推荐
- Ligerui table control grid changes the color of rows (cells) according to the content
- 我们为什么要推出Getaverse?
- deepin任务栏消失解决方法
- 安全的证券公司有哪些?我想在手机上买股票
- Software recommendation - office software
- EF LINQ Miscellany
- Why should we launch getaverse?
- 期盼已久全平台支持-开源IM项目OpenIM之uniapp更新
- Disassembly of Samsung Galaxy fold: the interior is extremely complex. Is the hinge the main cause of screen damage?
- 【机器学习基础】——另一个视角解释SVM
猜你喜欢

ArcGIS create vector

Simply use MySQL index

Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer

MySQL basic commands

Custom view - Custom button

Complete guide on how to prevent cross site scripting (XSS) attacks

EMQ Yingyun technology was listed on the 2022 "cutting edge 100" list of Chinese entrepreneurs

Envi SHP to ROI and mask the grid

1184. 公交站间的距离

TCP协议调试工具TcpEngine V1.3.0使用教程
随机推荐
Enter a URL to this page to show it. What happened in this process?
[technology] chat room demo of uniapp
Mobile phone comparison redmi note8 and realm x2
PS pull out logo
Summary of experience in using.Net test framework xUnit, mstest, specflow
Qt设计机器人仿真控制器——按键控制机器人关节转动
Solution to deepin taskbar disappearance
GEO satellite data download
JS, call in the for loop is asynchronously converted to synchronous execution
解决Eureka默认缓存配置导致时效性问题
Software recommendation - website construction
Intel plans to sell baseband chip business, but Apple has given up?
ZBar source code analysis - img_ scanner. c | [email protected]
Thinkphp3.2.5 cannot jump to external links
Qt信号和槽连接失败原因及解决办法
Software recommendation - Installation
Replace the image source of Debian full version with Alibaba cloud image source
Telephone system rules
Codeworks round 693 (Div. 3) C. long jumps problem solution
Qt键盘事件(二)——长按按键反复触发event事件问题解决