题解 | #字符统计#
字符统计
http://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.next();
Map<Character, Integer> map = new LinkedHashMap<>();
List<Character> list = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (map.containsKey(ch)) {
map.put(ch, map.get(ch) + 1);
} else {
map.put(ch, 1);
list.add(ch);
}
}
Collections.sort(list);
char[] chars = new char[list.size()];
for (int i = 0; i < chars.length; i++) {
chars[i] = list.get(i);
}
for (int i = 0; i < chars.length - 1; i++) {
for (int j = 0; j < chars.length - 1 - i; j++) {
if (map.get(chars[j]) < map.get(chars[j + 1])) {
char temp = chars[j];
chars[j] = chars[j+1];
chars[j+1] = temp;
}
}
}
for (int i = 0; i < chars.length; i++) {
if (i == chars.length - 1) {
System.out.println(chars[i]);
} else {
System.out.print(chars[i]);
}
}
}
}
}