输入一段文章,返回文章内出现次数最多的单词,和出现最少的单词。

输入一段文章,返回文章内出现次数最多的单词,和出现最少的单词。

加粗样式单词不分大小写,单词 以逗号,句号或空格分隔。
【测试用例1】
输入:hello world hello china hello world输出:hello china
【测试用例2】
输入:你好,世界。你好,中国。你好,世界。输出:你好 中国

import java.util.Scanner;
import java.util.HashMap;
import java.util.Map.Entry;

public class Main{
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int max=0;
        int min=100;
        HashMap<String ,Integer> cc=new HashMap<String, Integer>();
        StringBuffer sb = new StringBuffer();
        HashMap<String, Integer> has = new HashMap<String,Integer>();
        String[] sList = s.split(" |,|\\.|,|。");
        for(int i=0; i<sList.length; i++){
            if(!has.containsKey(sList[i])){
                has.put(sList[i], 1);
            }else{
                has.put(sList[i], has.get(sList[i])+1);
            }
        }
        for(Entry<String, Integer> entry:has.entrySet()){
            if(entry.getValue()>max){
                max=entry.getValue();
            }
            if(entry.getValue()<min){
                min=entry.getValue();
            }
        }
        for(Entry<String, Integer> entry:has.entrySet()){
            if(entry.getValue()==max){
                cc.put(entry.getKey(),entry.getValue());
            }
            if(entry.getValue()==min){
                cc.put(entry.getKey(),entry.getValue());
            }
        }
        for(Entry<String, Integer> entry:cc.entrySet()){
            if(entry.getValue()==max){
                System.out.print(entry.getKey()+" ");
            }
        }
        for(Entry<String, Integer> entry:cc.entrySet()){
            if(entry.getValue()==min){
                System.out.print(entry.getKey());
            }
        }
    }
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值