题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String string = scanner.nextLine(); char[] chr = string.toCharArray(); HashMap<Character, Integer> hashMap = new HashMap<>(); for (char c : chr) { hashMap.put(c, hashMap.getOrDefault(c, 0) + 1); } ArrayList<Map.Entry<Character, Integer>> list = new ArrayList<>(hashMap.entrySet()); list.sort(new Comparator<Map.Entry<Character, Integer>>() { @Override public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o2.getValue().compareTo(o1.getValue()); } }); String modifiedStrings = string; for (int i = list.size() - 1; i >= 0; i--) { char character = list.get(i).getKey(); modifiedStrings = modifiedStrings.replace(String.valueOf(character), ""); if (!list.get(i - 1).getValue().equals(list.get(i).getValue())) { break; } } System.out.println(modifiedStrings); } }