Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6.7笔试题 #10

Open
XingGuoZM opened this issue Jun 7, 2023 · 1 comment
Open

6.7笔试题 #10

XingGuoZM opened this issue Jun 7, 2023 · 1 comment

Comments

@XingGuoZM
Copy link
Owner

删除字符串中出现次数最少的字符

描述
实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,则把出现次数最少的字符都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。

数据范围:输入的字符串长度满足 1≤n≤20  ,保证输入的字符串中仅出现小写字母
输入描述:
字符串只包含小写英文字母, 不考虑非法输入,输入的字符串长度小于等于20个字节。

输出描述:
删除字符串中出现次数最少的字符后的字符串。

示例1
输入:
aabcddd
复制
输出:
aaddd
@XingGuoZM
Copy link
Owner Author

XingGuoZM commented Jun 7, 2023

let line = readline();

const map = {};
let min = 20;
for(let j=0;j<line.length;j++){
    map[line[j]]=(map[line[j]] || 0)+1;
}
Object.keys(map).forEach(item=>{
    min = Math.min(min,map[item]);
});

for(let i = 0;i<line.length;i++){
    if(map[line[i]] === min){
        line = line.replace(line[i],'');
        i--;
    }
}

console.log(line)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant