当前位置:网站首页>删除字符串中出现次数最少的字符【JS,Map排序,正则】
删除字符串中出现次数最少的字符【JS,Map排序,正则】
2022-07-04 17:16:00 【qq_22841387】
问题描述
删除字符串中出现次数最少的字符_牛客题霸_牛客网 (nowcoder.com)
题目几个重点
- 删除所有次数为最少的字符
- 保证原来序列
解决代码
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);
刷题总结
1.初始化各个字符个数
直接通过while循环遍历value原始字符串,while(value[i]),因为value对应位置i不存在是会以undefined存在,while中会自动转换成boolean类型的值,对应的undefined的就是falsy(假值)
- 判断是否
map中是否存在- 若存在,对应的数字++
- 若不存在,则通过set设置值
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排序
map排序大致思想:通过转换成数组,调用数组的sort,并重写sort默认排序规则
- 将map转换为数组
let arr = Array.from(map类型的变量)
- 重写数组排序规则
按value排序——降序
arr.sort( ( a , b) => {
return a[1] - b[1];
})
按key排序
arr.sort( ( a , b ) => {
return a[0] - b[0]
})
3.replace替换所有——正则
由于牛客不支持replaceAll方法,所以需要自己实现一个。
而且题目中要求删除所有最小值,所以我们需要对map进行循环遍历,通过排序找到最小值后,判断每一个key的value是否与最小值相等
若相等,对原串进行替换
if(strMap.get(key) === minNum){
const reg = new RegExp(key , 'g');
value = value.replace(reg,'');
}
这里也有几个小知识点
- 正则全局删——
/g - 删除value中的值——
replace(reg ,'') - replace返回替换的字符串
边栏推荐
猜你喜欢

Principle and application of ThreadLocal
![[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors

能源行业的数字化“新”运维

2022 ByteDance daily practice experience (Tiktok)

What types of Thawte wildcard SSL certificates provide

Scala基础教程--17--集合
![[HCIA continuous update] WAN technology](/img/31/8e9ed888d22b15eda5ddcda9b8869b.png)
[HCIA continuous update] WAN technology

Mysql5.7 installation tutorial graphic explanation

力扣刷题日记/day6/6.28

谷粒商城(一)
随机推荐
Scala basic tutorial -- 19 -- actor
1、 Introduction to C language
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
Deleting nodes in binary search tree
被忽视的问题:测试环境配置管理
Scala basic tutorial -- 20 -- akka
Machine learning concept drift detection method (Apria)
Wireshark packet capturing TLS protocol bar displays version inconsistency
如何提高开发质量
ThreadLocal原理与使用
Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
中国农科院基因组所汪鸿儒课题组诚邀加入
输入的查询SQL语句,是如何执行的?
C language printing exercise
Microservice architecture debate between radical technologists vs Project conservatives
File processing examples of fopen, FREAD, fwrite, fseek
力扣刷题日记/day6/6.28
基于NCF的多模块协同实例
蓝桥:合根植物
Li Kou brush question diary /day2/2022.6.24