题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 // while (in.hasNextInt()) { // 注意 while 处理多个 case // int a = in.nextInt(); // int b = in.nextInt(); // System.out.println(a + b); // } String line = in.nextLine(); Node[] nodes = new Node[line.length()]; int index = 0; // char[] minChar = new char[line.length()]; for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); boolean has = false; for (int j = 0; j < index; j++) { if (nodes[j].c == c) { nodes[j].n++; has = true; break; } } if (has) { continue; } else { Node node = new Node(); node.c = c; nodes[index] = node; index++; } } for (int i = 1; i <= 20; i++) { boolean find = false; for (int j = 0; j < nodes.length; j++) { if (nodes[j] == null) { break; } if (i == nodes[j].n) { line = line.replaceAll(Character.toString(nodes[j].c), ""); find = true; } } if (find) { break; } } System.out.print(line); } } class Node { protected char c; protected int n = 1; }