题解 | #字符统计#

字符统计

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 str = sc.nextLine();

        Map<Character, Integer> map = new TreeMap<>();
        for(int i=0; i<str.length(); i++){
            //Set<Character> keys = map.keySet();
            char key = str.charAt(i);
            //if(keys.contains(key)){
            if(map.containsKey(key)){
                map.replace(key, map.get(key)+1);
            }else{
                map.put(key, 1);
            }
        }
        
        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--;
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务