描述
对于给定的仅由小写字母构成的字符串,删除字符串中出现次数最少的字符。输出删除后的字符串,字符串中其它字符保持原来的顺序。
特别地,若有多个字符出现的次数都最少,则把这些字符都删除。
输入描述:
在一行上输入一个长度为 1≦length(s)≦20 ,仅由小写字母构成的字符串 s ,代表待处理的字符串。
输出描述:
在一行上输出一个字符串,代表删除后的答案。保证这个字符串至少包含一个字符。
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 charCount = {};
for (const char of line) {
charCount[char] = (charCount[char] || 0) + 1;
}
// 找到最小的出现次数
const minCount = Math.min(...Object.values(charCount));
// 删除所有出现次数等于最小值的字符
let result = "";
for (const char of line) {
if (charCount[char] !== minCount) {
result += char;
}
}
console.log(result);
}
})();