码迷,mamicode.com
首页 > 其他好文 > 详细

删除字符串中出现次数最少的字符

时间:2015-09-04 11:09:52      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 1 mport java.util.Collection;
 2 import java.util.Collections;
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 import java.util.Scanner;
 6 
 7 
 8 public class Main
 9 {
10 
11      public static void main(String[] args)
12      {//abcdabcddd
13           StringBuilder res = new StringBuilder(256);
14           Scanner sc =new Scanner(System.in);
15           String input = sc.nextLine();
16          
17           if(input.length() > 20) return ;
18           HashMap<Character, Integer> map = new HashMap<Character, Integer>();
19           //count character times
20           char[] chs = input.toCharArray();
21           for(char ch : chs)
22           {
23                if(!map.containsKey(ch))
24                {
25                     map.put(ch,1);
26                }
27                else
28                {
29                     map.put(ch,map.get(ch)+1);
30                }
31           }
32          
33           int min = Collections.min(map.values());
34           //delete
35           for(char ch :chs)
36           {
37                if(min != map.get(ch))
38                     res.append(ch);
39           }
40          
41 
42           System.out.println(res.toString());   
43          
44      }
45 
46 }

 

删除字符串中出现次数最少的字符

标签:

原文地址:http://www.cnblogs.com/sweetculiji/p/4781301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!