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

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

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);
    }
} 

}

我居南半坡 文章被收录于专栏

多刷题,积蓄力量,欢迎讨论

全部评论

相关推荐

用微笑面对困难:985只有在应届生里面的优势是断层的在社招或者更远的工作中算是后续能力优先级
工作压力大,你会干什么?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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