题解 | #统计一句话中重复单词的个数#

统计一句话中重复单词的个数

https://www.nowcoder.com/practice/2128e598b5a5407195c31175b5b33360

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        Map<Character, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);
            String s = String.valueOf(c);
            if (!s.equals(" ")) {
                if (map.containsKey(c)) {
                    int count = map.get(c) + 1;
                    map.put(c, count);
                } else {
                    map.put(c, 1);
                }
            }
        }
        Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entrys) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
面试了几家,全程问项目,八股一点都不问,可惜准备了这么久
独角仙梦境:现在感觉问八股像是中场休息一样的,问几个八股放松一下再上强度
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务