题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
char[] chrs = str.toCharArray(); // 将字符串转换为 char 类型的数组
int[] nums = new int[26]; // 定义一个整型数组,用于存放每一个字符出现的次数
int minChar = str.length(); // 定义一个整型变量,用于存放字符串中,出现次数最少的字符,出现的次数
for (int i = 0; i < chrs.length; i++) {
nums[chrs[i] - 'a']++;
}
for (int i = 0; i < chrs.length; i++) {
minChar = Math.min(minChar, nums[chrs[i] - 'a']);
}
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < chrs.length; i++) {
if (nums[chrs[i] - 'a'] > minChar) {
sb.append(chrs[i]);
}
}
System.out.println(sb);
}
}