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

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

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

import java.util.*;
public class Main{
    public Main(){
        
    }
    public String delete(String str){
        // Map记录每个字母的次数
        Map<Character,Integer>map = new HashMap<>();
        for(char ch : str.toCharArray()){
            map.put(ch,map.getOrDefault(ch,0)+1);
        }
        // 快速找出最少次数
        int min = Integer.MAX_VALUE;
        for(int times : map.values()){
            min = Math.min(min,times);
        }
        StringBuilder res = new StringBuilder();
        for(char ch : str.toCharArray()){
            if(map.get(ch)!=min){
                res.append(ch);
            }
        }
        return res.toString();
    }
    public static void main(String[] args){
        Main solution = new Main();
        Scanner in = new Scanner(System.in);
        while(in.hasNextLine()){
            String str = in.nextLine();
            String res = solution.delete(str);
            System.out.println(res);
        }
    }
}
全部评论

相关推荐

09-23 14:45
贵州大学 财务
勇敢求职牛牛:怎么9.2佬人手一个中信证券实习
点赞 评论 收藏
分享
在看牛客的社畜很积极:身高体重那一行信息去掉,学校那一行的信息放上面,找半天都没找到你是哪个学校什么专业的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务