Java写题解的第1天 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
通过桶排序获取字符串中出现最少的字符,
由于一个字符串中可能同时存在多个出现次数最少的字符,
因此再次遍历字符串每个字符并存入stringbuilder中
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = ""; while ((s = br.readLine()) != null) { int[] counter = new int[26]; for(char c: s.toCharArray()) { counter[c-'a'] ++; } int least = Integer.MAX_VALUE; for (int n: counter) { if (n != 0 && n < least) { least = n; } } StringBuilder sb = new StringBuilder(); for (char c: s.toCharArray()) { if (counter[c-'a'] != least) { sb.append(c); } } System.out.println(sb.toString()); } br.close(); } }