首页 > 其他 > 详细

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

时间:2015-09-04 11:08:10      阅读:279      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!