题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
import java.util.*; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); HashMap<Character,Integer> map=new HashMap<Character,Integer>(); String s=in.next(); int len=s.length(); int min=20; for(int i=0;i<len;i++){ char c=s.charAt(i); if(map.containsKey(c)){ map.put(c,map.get(c)+1); }else{ map.put(c,1); } } TreeSet<Integer> tree=new TreeSet<Integer>(); for(int n:map.values())tree.add(n); min=tree.first(); StringBuffer sb=new StringBuffer(""); for(Character c:map.keySet()){ if(map.get(c)==min)sb.append(c); } for(int i=0;i<sb.length();i++){ s=s.replace(Character.toString(sb.charAt(i)),""); } System.out.print(s); } }