题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.Scanner;
//将所有出现的小写字母计数,a[0]代表a,a[25]代表z
//找出最少但大于0的数
//找是否有重复数,如有,一并记录
//对被标记为最少的字母进行清除。
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = new String(sc.nextLine());
System.out.println(fun(str));
}
public static String fun(String str) {
int[] arr = new int[26];
for (int i = 0; i < str.length(); i++) {
int chtemp = str.charAt(i) - 97;
arr[chtemp]++;
}
int minvalue = 0;
for (int i = 0; i < 26; i++) {
if ((arr[i] > 0 && arr[i] < minvalue) || (arr[i] > 0 && minvalue == 0))
minvalue = arr[i];
}
int[] arrtemp = new int[26];
for (int i = 0; i < 26; i++) {
if (arr[i] == minvalue) {
arrtemp[i] = 1;
}
}
char[] ch = new char[str.length()];
int temp1 = 0;
int j = 0;
for (int i = 0; i < str.length(); i++) {
if (arrtemp[str.charAt(i) - 97] != 1) {
ch[j] = str.charAt(i);
j++;
temp1++;
}
}
char[] ch1 = new char[temp1];
for (int i = 0; i < temp1; i++) {
ch1[i] = ch[i];
}
return new String(ch1);
}
}
查看13道真题和解析