当前位置:网站首页>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
边栏推荐
- "Only one trip", active recommendation and exploration of community installation and maintenance tasks
- Reflection (I)
- Prometheus installation
- Basic use of kotlin
- 明明的随机数
- 牛客小白月赛7 谁是神箭手
- c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
- On communication bus arbitration mechanism and network flow control from the perspective of real-time application
- TCP waves twice, have you seen it? What about four handshakes?
- HMM隐马尔可夫模型最详细讲解与代码实现
猜你喜欢

CANN算子:利用迭代器高效实现Tensor数据切割分块处理

Swagger突然发癫

Euler function

The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!

TCP两次挥手,你见过吗?那四次握手呢?

实战模拟│JWT 登录认证

English语法_名词 - 使用

92. (cesium chapter) cesium building layering

mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总

Online text line fixed length fill tool
随机推荐
Reflection (I)
C# 使用StopWatch测量程序运行时间
abc229 总结(区间最长连续字符 图的联通分量计数)
1005 Spell It Right(20 分)(PAT甲级)
BCG 使用之CBCGPProgressDlgCtrl進度條使用
Thinking on demand development
Chrome development tool: what the hell is vmxxx file
Pointnet / pointnet++ point cloud data set processing and training
1007 maximum subsequence sum (25 points) (PAT class a)
牛客小白月赛7 F题
Prometheus installation
@transactional滥用导致数据源连接池耗尽问题
求2的n次方
1006 sign in and sign out (25 points) (PAT class a)
kotlin 基本使用
1002. A+b for Polynomials (25) (PAT class a)
Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading
YOLOv5s-ShuffleNetV2
1005 spell it right (20 points) (pat a)
线上数据库迁移的几种方法