题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String code = in.next(); HashMap<Character,Integer> memo = mapGenerator(code); int min = minValueCounter(memo); System.out.println(filterStringByCount(memo,code,min)); } } // 对所有的元素个数进行统计 private static HashMap<Character, Integer> mapGenerator(String input) { HashMap<Character, Integer> memo = new HashMap<>(); for (char character : input.toCharArray()) { memo.put(character, memo.getOrDefault(character, 0) + 1); } return memo; } // 寻找最小的出现次数 private static int minValueCounter(Map<Character, Integer> record) { int min = Integer.MAX_VALUE; for (Map.Entry<Character, Integer> entry : record.entrySet()) { min = Math.min(min, entry.getValue()); } return min; } //返回筛选后的字符串 private static String filterStringByCount(Map<Character, Integer>record, String input, int min) { StringBuilder res = new StringBuilder(); for (char c : input.toCharArray()) { if (record.get(c) > min) { res.append(c); } } return res.toString(); } }#哈希表##删除字符串中出现次数最少的字符#