题解 | #字符统计#

字符统计

https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        TreeMap<Character,Integer> map=new TreeMap<>();
        for (int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if (!map.containsKey(c)){
                map.put(c,1);
            }else {
                map.put(c,map.get(c)+1);
            }
        }
        Collection<Integer> values = map.values();
        Set<Character> keySet = map.keySet();
        StringBuilder sb=new StringBuilder();
        int max = 0;
        for (int val:map.values()) {
            if(val>max)
                max = val;
        }
        while (max>0){
            for (char key: map.keySet()){
                if(map.get(key)==max)
                    System.out.print(key);
            }
            max--;
        }
    }
}

全部评论

相关推荐

猿辅导 Java后端日常实习 800一天
点赞 评论 收藏
转发
宇信外包 Java 7.5k
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务