当前位置:网站首页>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
mapWhether 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
边栏推荐
- HDU 1372 & POJ 2243 Knight moves (breadth first search)
- abc229 总结(区间最长连续字符 图的联通分量计数)
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- HMM hidden Markov model and code implementation
- "Only one trip", active recommendation and exploration of community installation and maintenance tasks
- 明明的随机数
- Stream stream
- Dark horse programmer - software testing - stage 08 2-linux and database-23-30-process port related, modify file permissions, obtain port number information, program and process related operations, Li
- 【毕业季】绿蚁新醅酒,红泥小火炉。晚来天欲雪,能饮一杯无?
- 需求开发思考
猜你喜欢

实战模拟│JWT 登录认证

勾股数规律(任意三个数能够满足勾股定理需要满足的条件)

Online sql to excel (xls/xlsx) tool

c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)

What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!

YOLOv5s-ShuffleNetV2

Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,

Multi table operation inner join query
牛客小白月赛7 谁是神箭手

欧拉函数
随机推荐
Multi table operation inner join query
Online sql to excel (xls/xlsx) tool
@transactional滥用导致数据源连接池耗尽问题
How to use async Awati asynchronous task processing instead of backgroundworker?
项目中遇到的线上数据迁移方案1---总体思路整理和技术梳理
双冒号作用运算符以及命名空间详解
需求开发思考
[QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
输入的查询SQL语句,是如何执行的?
Cbcgpprogressdlgctrl progress bar used by BCG
Kotlin cycle control
English语法_名词 - 使用
做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
Matrix flip (array simulation)
Add namespace declaration
Is it safe to open an account at Great Wall Securities? How to open an account when buying stocks
HDU 1097 A hard puzzle
明明的随机数