当前位置:网站首页>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
map
Whether 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
边栏推荐
猜你喜欢
Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
TCP waves twice, have you seen it? What about four handshakes?
HMM hidden Markov model and code implementation
Abc229 summary (connected component count of the longest continuous character graph in the interval)
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?
解密函数计算异步任务能力之「任务的状态及生命周期管理」
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
C语言-入门-基础-语法-流程控制(七)
The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize
随机推荐
Educational Codeforces Round 22 E. Army Creation
kotlin 类和对象
Free soldier
2022 Health Exhibition, Beijing Health Expo, China Health Exhibition, great health exhibition November 13
JVM系列之对象的创建
Creation of JVM family objects
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Educational Codeforces Round 22 E. Army Creation
Multi table operation - external connection query
HMM隐马尔可夫模型最详细讲解与代码实现
Prometheus installation
多表操作-外连接查询
TCP两次挥手,你见过吗?那四次握手呢?
HMM hidden Markov model and code implementation
双冒号作用运算符以及命名空间详解
1003 emergency (25 points) (PAT class a)
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
What are the consequences of closing the read / write channel?
【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
需求开发思考