题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
- 先遍历一次,对出现的字母进行计数
- 再遍历一次计数,得到最小次数
- 遍历字符串,对高于计数的进行输出
import java.util.Scanner;
/**
* class com.sloera.nowcoder
* user sloera
* date 2022/2/23
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
final String alphabet = in.nextLine();
resolve(alphabet);
System.out.println();
}
}
private static void resolve(String alphabet) {
int[] count = new int[26];
for (int i = 0; i < alphabet.length(); i++) {
count[alphabet.charAt(i)-'a']++;
}
int min = alphabet.length();
for (int i = 0; i < count.length; i++) {
if (count[i] > 0 && count[i] < min) {
min = count[i];
}
}
for (int i = 0; i < alphabet.length(); i++) {
if (count[alphabet.charAt(i) - 'a'] > min) {
System.out.print(alphabet.charAt(i));
}
}
}
}