题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
哈希表
这里使用数组代替哈希表,开销更小,性能更好
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); char[] arr = br.readLine().toCharArray(); // 统计每个字母出现的次数 int[] freq = new int[26]; for (char c : arr) { freq[c - 'a']++; } // 最小次数 int minFreq = arr.length; for (int f : freq) { if (f > 0) minFreq = Math.min(minFreq, f); } for (char c : arr) { // 跳过出现次数最少的字符 if (freq[c - 'a'] == minFreq) { continue; } pw.print(c); } pw.println(); pw.flush(); pw.close(); br.close(); } }