import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String string = in.nextLine();
Map<Character, Integer> map = new HashMap<>();
for (char c : string.toCharArray()) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
list.sort((o1, o2) -> o1.getValue() - o2.getValue());
int num = list.get(0).getValue();
Set<Character> set = new HashSet<>();
set.add(list.get(0).getKey());
for (int i = 1; i < list.size(); i++) {
if (list.get(i).getValue() == num) {
set.add(list.get(i).getKey());
} else {
break;
}
}
StringBuilder sb = new StringBuilder();
for (char c : string.toCharArray()) {
if (!set.contains(c)) {
sb.append(c);
}
}
System.out.println(sb);
}
}
我是先统计每个字符出现的次数,然后找出出现次数最少的字符,然后再将原字符串中不是出现次数最少的字符重新拼接返回

京公网安备 11010502036488号