题解 | #删除字符串中出现次数最少的字符#

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

https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        Map<Character,Integer> map = new HashMap<>();
        for(char ch : str.toCharArray()){
            if(map.containsKey(ch)){
                map.put(ch,map.get(ch)+1);
            }else{
                map.put(ch,1);
            }
        }
        int minTime = 20;
        for(char key : map.keySet()){
            int time = map.get(key);
            if(time < minTime){
                minTime = time;
            }
        }
        for(char key : map.keySet()){
            int time = map.get(key);
            if(time == minTime){
                map.put(key,0);
            }
        }
        StringBuilder builder = new StringBuilder();
        for(char ch : str.toCharArray()){
            if(map.get(ch) != 0){
                builder.append(ch);
            }
        }
        System.out.println(builder.toString());
        }
}

全部评论

相关推荐

03-11 14:28
浙江大学 设计
牛客小黄鱼:代入一下,独居女生会觉得有点可怕
点赞 评论 收藏
分享
03-19 10:07
已编辑
广东药科大学 golang
Yki_:你倒是进一个面啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务