题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.Scanner; import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNextLine()){ Map<Character, Integer> map = new HashMap<>(); String input = scanner.nextLine(); if(input == null || "".equals(input)){ break; } char[] array = input.toCharArray(); for(Character temp : array){ if(map.get(temp) == null || map.get(temp) == 0){ map.put(temp, 1); continue; } map.put(temp, map.get(temp)+1); } if(map.isEmpty()){ return; } int less = map.entrySet().stream().sorted(Comparator.comparing(e->e.getValue().intValue())).findFirst().get().getValue(); for(Character character : map.keySet()){ if(map.get(character) == less){ input = input.replaceAll(character.toString(), ""); } } System.out.println(input); } } }