题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
int n = 0;
for (int j = 0; j < str.length(); j++) {
if (str.charAt(j) == c) {
n++;
}
}
map.put(i, n);
}
int min = Integer.MAX_VALUE;
for (Integer key : map.keySet()) {
if (min > map.get(key)) {
min = map.get(key);
}
}
List<Integer> list = new ArrayList<>();
for (Integer key : map.keySet()) {
if (min == map.get(key)) {
list.add(key);
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (list.contains(i)) {
continue;
}
sb.append(str.charAt(i));
}
System.out.println(sb);
}
}
腾讯公司福利 1145人发布
查看30道真题和解析