题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.time.LocalDateTime; import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream; import static java.util.stream.Stream.*; public class Main { public static void main(String[] args) throws IOException { //testCompletePack(); testTh(); } private static void testTh() throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String str; HashMap<Character, Integer> hp = new HashMap<>(); int count = 0; int min = Integer.MAX_VALUE; while ((str = bf.readLine()) != null) { char[] chars = str.toCharArray(); char[] temp = new char[chars.length]; for (int i = 0; i < chars.length; i++) { if (hp.containsKey(chars[i])) { count = hp.get(chars[i]); count++; hp.put(chars[i], count); } else { hp.put(chars[i], 1); } } Set<Map.Entry<Character, Integer>> entries = hp.entrySet(); int i = 0; for (Map.Entry<Character, Integer> entry : entries) { if (entry.getValue() < min) { min = entry.getValue(); temp[i] = entry.getKey(); } } i++; for (Map.Entry<Character, Integer> entry : entries) { if (entry.getValue() == min) { temp[i] = entry.getKey(); i++; } } for (int i1 = 0; i1 < temp.length; i1++) { String s = String.valueOf(temp[i1]); str = str.replaceAll(s, ""); } Stream.of(str).forEach(System.out::println); } } }