题解 | #字符统计#

字符统计

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

import java.util.TreeMap;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		String string=input.nextLine();
		TreeMap<String, Integer> map=new TreeMap<>();
		for (int i = 0; i < string.length(); i++) {
			String s=string.substring(i,i+1);
			int num=1;
			if (map.containsKey(s)) {
				map.put(s, map.get(s)+1);
			}else {
				map.put(s, num);
			}
		}
//		System.out.println(map);
		
		int max=0;
		for (String string2:map.keySet()) {
			if (map.get(string2)>max) {
				max=map.get(string2);
			}			
		}
//		System.out.println(max);
		
		for (int i = max; i >0; i--) {
			for (String string2:map.keySet()) {
				if (map.get(string2)==i) {
					System.out.print(string2);
				}			
			}
			
		}
		
	}
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务