题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
System.out.println(solution1(str));
}
protected static String solution1(String str) {
Map<Character, Integer> map = new HashMap<>(str.length());
for (char c : str.toCharArray()) {
map.compute(c, (k, v) -> v == null ? 1 : v + 1);
}
Integer min = map.values().stream().min(Integer::compare).orElse(0);
Set<Character> set = new HashSet<>();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue().equals(min)) {
set.add(entry.getKey());
}
}
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if (!set.contains(c)) {
sb.append(c);
}
}
return sb.toString();
}
}
