-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountChar.java
More file actions
36 lines (32 loc) · 990 Bytes
/
CountChar.java
File metadata and controls
36 lines (32 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.hw;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/**
* @author: yhl
* @DateTime: 2020/5/27 17:30
* @Description:
*/
public class CountChar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
final String s = sc.next();
Map<Character, Integer> map = new TreeMap<>((o1, o2) -> o2 - o1);
for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
} else {
map.put(s.charAt(i), 1);
}
}
final Set<Character> characters = map.keySet();
StringBuilder sb = new StringBuilder();
for (Character c : characters) {
sb.append(c);
}
System.out.println(sb.toString());
}
}
}