题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main{ public static void main(String[] args) throws Exception{ BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); String str = read.readLine(); // 使用数组最为hash表 int[] alphabet = new int[26]; for(char ch : str.toCharArray()){ alphabet[ch - 'a']++; } int min = Integer.MAX_VALUE; // 找到最少的字母出现次数 for(int i : alphabet){ if(i != 0){ min = Math.min(min, i); } } // 对于出现所有出现min次的字母,用空字符串进行替换 for(int i = 0; i < alphabet.length; ++i){ if(alphabet[i] == min){ Character r = (char) ('a' + i); str = str.replace(r.toString(), ""); } } System.out.println(str); } }