当前位置:网站首页>Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
Delete the characters with the least number of occurrences in the string [JS, map sorting, regular]
2022-07-04 20:04:00 【qq_ twenty-two million eight hundred and forty-one thousand thr】
Problem description
Delete the least frequent character in the string _ Niuke Tiba _ Cattle from (nowcoder.com)
Several key points of the topic
- Delete all characters with the least number of times
- Ensure the original sequence
Solve the code
var value = readline();
let strMap = new Map();
let singleValue , cnt;
let i = 0 ;
while(value[i]){
singleValue = value[i];
if(strMap.has(singleValue)){
cnt = strMap.get(singleValue);
cnt ++;
strMap.set(singleValue , cnt);
}else{
strMap.set(singleValue , 1);
}
i ++;
}
let arr = Array.from(strMap)
// console.log(Math.min(...arr))
arr.sort((a , b) => {
return a[1] - b[1];
})
let minNum = arr[0][1];
let key
for(const item of strMap ){
key = item[0]
if(strMap.get(key) === minNum){
const reg = new RegExp(key , 'g')
value = value.replace(reg,'');
}
}
console.log(value);
Sum up the questions
1. Initialize the number of characters
Directly through while Loop traversal value Original string ,while(value[i])
, because value Corresponding position i Does not exist will be undefined
There is ,while Will be automatically converted to boolean Type value , Corresponding undefined is falsy( False value )
- Determine whether
map
Whether there is- if There is , Corresponding number ++
- if non-existent , Through set Set the value
while(value[i]){
singleValue = value[i];
if(strMap.has(singleValue)){
cnt = strMap.get(singleValue);
cnt ++;
strMap.set(singleValue , cnt);
}else{
strMap.set(singleValue , 1);
}
i ++;
}
2.map Sort
map Sort the general idea : By converting to an array , Call array sort, And rewrite sort Default collation
- take map Convert to array
let arr = Array.from(map Variable of type )
- Rewrite array collation
Press value Sort —— Descending
arr.sort( ( a , b) => {
return a[1] - b[1];
})
Press key Sort
arr.sort( ( a , b ) => {
return a[0] - b[0]
})
3.replace Replace all —— Regular
Because Niuke doesn't support replaceAll Method , So you need to implement a .
And all minimum values are required to be deleted in the title , So we need to deal with map Conduct Loop traversal
, Find minimum value
after , Judge every key Of value Whether it is equal to the minimum
If equal , Replace the original string
if(strMap.get(key) === minNum){
const reg = new RegExp(key , 'g');
value = value.replace(reg,'');
}
Here are also a few tips
- Regular global deletion ——
/g
- Delete value The value in ——
replace(reg ,'')
- replace Returns the replacement string
边栏推荐
- C # use stopwatch to measure the running time of the program
- 上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
- Introduction to ACM combination counting
- Pointnext: review pointnet through improved model training and scaling strategies++
- 1008 Elevator(20 分)(PAT甲级)
- Wireshark network packet capture
- 牛客小白月赛7 谁是神箭手
- kotlin 基本数据类型
- 1009 Product of Polynomials(25 分)(PAT甲级)
- kotlin 基本使用
猜你喜欢
Actual combat simulation │ JWT login authentication
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
In operation (i.e. included in) usage of SSRs filter
YOLOv5s-ShuffleNetV2
BCG 使用之新建向导效果
ACM组合计数入门
Creation of JVM family objects
"Only one trip", active recommendation and exploration of community installation and maintenance tasks
多表操作-内连接查询
HMM隐马尔可夫模型最详细讲解与代码实现
随机推荐
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
BCG 使用之CBCGPProgressDlgCtrl進度條使用
Cbcgpprogressdlg progress bar used by BCG
HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
Free soldier
数据集划分
多表操作-外连接查询
Stream stream
牛客小白月赛7 I 新建 Microsoft Office Word 文档
欧拉函数
西门子HMI下载时提示缺少面板映像解决方案
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
Educational codeforces round 22 E. Army Creation
Chrome development tool: what the hell is vmxxx file
明明的随机数
Educational Codeforces Round 22 E. Army Creation
双冒号作用运算符以及命名空间详解
C语言-入门-基础-语法-流程控制(七)
Double colon function operator and namespace explanation
Development and construction of DFI ecological NFT mobile mining system