题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String sinput = in.nextLine(); Map<String, Integer> map = new HashMap<>(sinput.length()); char[] arr = sinput.toCharArray(); for (char ch : arr) { map.put(String.valueOf(ch), map.get(String.valueOf(ch)) == null ? 1 : map.get(String.valueOf(ch)) + 1); } //System.out.println(map); Collection<Integer> c = map.values(); Integer i = Collections.min(c); for (Map.Entry<String, Integer> e : map.entrySet()) { if (i == e.getValue()) { sinput = sinput.replaceAll(e.getKey(), ""); } } System.out.println(sinput); } } }