输入一段文章,返回文章内出现次数最多的单词,和出现最少的单词。
加粗样式单词不分大小写,单词 以逗号,句号或空格分隔。
【测试用例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());
}
}
}
}