题解 | #删除字符串中出现次数最少的字符#

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

http://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

使用hashMap完成

import java.util.*;
public class Main {
        public static void main(String[] args) {
            Scanner sc =new Scanner(System.in);
            while (sc.hasNextLine()) {
                String str = sc.nextLine();
                int length = str.length();
                HashMap<Character,Integer> map = new HashMap<Character,Integer>();
                HashSet<Character> set = new HashSet<Character>();
                for (int i = 0; i < length; i++) {
                    if (!map.containsKey(str.charAt(i))) {
                        map.put(str.charAt(i), 1);
                    } else {
                        map.put(str.charAt(i), map.get(str.charAt(i)) + 1);
                    }
                }

                int min = length;
                 for(Map.Entry<Character,Integer> item : map.entrySet()) {
                    if (item.getValue() < min) {
                       min = item.getValue();
                    }
                }
                
                for(Map.Entry<Character,Integer> item : map.entrySet()) {
                    if (item.getValue() == min) {
                        str = str.replace(item.getKey().toString(),"");
                    }
                }
                System.out.println(str);
            }
        }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客企业服务