题解 | #字符统计#
字符统计
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--; } } }