当前位置:网站首页>删除字符串中出现次数最少的字符【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返回替换的字符串
边栏推荐
- 能源行业的数字化“新”运维
- [go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
- . Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
- Wireshark抓包TLS协议栏显示版本不一致问题
- Scala basic tutorial -- 12 -- Reading and writing data
- 同事悄悄告诉我,飞书通知还能这样玩
- [go ~ 0 to 1] read, write and create files on the sixth day
- Scala基础教程--16--泛型
- Thawte通配符SSL证书提供的类型有哪些
- 【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
猜你喜欢

Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction

力扣刷题日记/day4/6.26

An example of multi module collaboration based on NCF

Journal des problèmes de brosse à boutons de force / day6 / 6.28

基于lex和yacc的词法分析器+语法分析器

力扣刷题日记/day1/2022.6.23

Blue bridge: sympodial plant

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”

能源行业的数字化“新”运维
随机推荐
Interpretation of SIGMOD '22 hiengine paper
力扣刷题日记/day1/2022.6.23
TCP waves twice, have you seen it? What about four handshakes?
力扣刷题日记/day2/2022.6.24
【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
How to modify icons in VBS or VBE
一、C语言入门基础
Scala基础教程--15--递归
Li Kou brush question diary /day1/2022.6.23
Blue bridge: sympodial plant
大厂面试总结大全二
Scala basic tutorial -- 15 -- recursion
Scala基础教程--18--集合(二)
Esp32-c3 introductory tutorial questions ⑫ - undefined reference to ROM_ temp_ to_ power, in function phy_ get_ romfunc_ addr
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
TCP两次挥手,你见过吗?那四次握手呢?
uni-app与uviewUI实现仿小米商城app(附源码)
MXNet对GoogLeNet的实现(并行连结网络)