题解 | #字符统计#
字符统计
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
//将字符存入TreeMap
TreeMap<Character,Integer> map = new TreeMap<>();
for(int i = 0; i < str.length(); i++){
if(!map.containsKey(str.charAt(i))){
map.put(str.charAt(i),1);
}else{
map.put(str.charAt(i),map.get(str.charAt(i)) + 1);
}
}
//找到max值
int max = 0;
for(int ch : map.values()){
if(max < ch){
max = ch;
}
}
//输出
while(max != 0){
for(char ch : map.keySet()){
if(map.get(ch) == max){
System.out.print(ch);
}
}
max--;
}
}
}