当前位置:网站首页>删除字符串中出现次数最少的字符【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返回替换的字符串
边栏推荐
- IBM WebSphere MQ检索邮件
- VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
- Scala basic tutorial -- 19 -- actor
- ESP32-C3入门教程 问题篇⑫——undefined reference to rom_temp_to_power, in function phy_get_romfunc_addr
- 力扣刷题日记/day7/2022.6.29
- Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
- 力扣刷题日记/day8/7.1
- Mysql5.7 installation tutorial graphic explanation
- Li Kou brush question diary /day8/7.1
- [2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
猜你喜欢

Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project

力扣刷题日记/day8/7.1

Wireshark packet capturing TLS protocol bar displays version inconsistency

Scala基础教程--19--Actor

NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读

How is the entered query SQL statement executed?

力扣刷题日记/day7/6.30

Mxnet implementation of googlenet (parallel connection network)

爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用

Li Kou brush question diary /day3/2022.6.25
随机推荐
Scala basic tutorial -- 18 -- set (2)
Microservice architecture debate between radical technologists vs Project conservatives
Scala basic tutorial -- 12 -- Reading and writing data
How to modify icons in VBS or VBE
发送和接收IBM WebSphere MQ消息
The block:usdd has strong growth momentum
激进技术派 vs 项目保守派的微服务架构之争
Caché JSON 使用JSON适配器
Halcon template matching
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
[211] go handles the detailed documents of Excel library
工厂从自动化到数字孪生,图扑能干什么?
Basic tutorial of scala -- 16 -- generics
完善的js事件委托
TCP两次挥手,你见过吗?那四次握手呢?
Li Kou brush question diary /day5/2022.6.27
Scala basic tutorial -- 17 -- Collection
Scala basic tutorial -- 15 -- recursion
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
力扣刷题日记/day2/2022.6.24