当前位置:网站首页>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 )

Falsy - The glossary | MDN (mozilla.org)

  • 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
原网站

版权声明
本文为[qq_ twenty-two million eight hundred and forty-one thousand thr]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041715558158.html