题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
// Write your code here
while ((line = await readline())) {
const countMap = new Map();
// 获取各个字符串的计数
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (!countMap.has(char)) {
countMap.set(char, 1);
} else {
countMap.set(char, countMap.get(char) + 1);
}
}
// 找出最小的那个 迭代 Map
const entries = Array.from(countMap.entries()).sort((a, b) => a[1] - b[1]);
// 有重复
const [_, minCount] = entries[0];
for (const entry of entries) {
if (entry[1] === minCount) {
line = line.replace(new RegExp(entry[0], "g"), "");
continue;
}
break;
}
console.log(line);
}
})();
海康威视公司福利 1154人发布
查看14道真题和解析