*LeetCode--Most Common Word

Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.  It is guaranteed there is at least one word that isn't banned, and that the answer is unique.

Words in the list of banned words are given in lowercase, and free of punctuation.  Words in the paragraph are not case sensitive.  The answer is in lowercase.

Example:
Input: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation: 
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"), 
and that "hit" isn't the answer even though it occurs more because it is banned.

 

需要注意的点:

1.在java中防止转义字符是 要使用 \ 不是 /

2.还有就是如果字符串中包含不需要的某几个字符时,可以使用replaceAll()方法去掉

public String replaceAll(String regex,
                         String replacement)
其中第一个参数是一个正则表达式,表示替换和regex中相匹配的所有字符串。

3.将数组转成集合List需要借助Arrays工具类

4.map的一种简单判断是否存在,且放入键值对的方法:

map.put(word, map.getOrDefault(word, 0) + 1);

map.getOrDefault(key, defaultValue)“键”没找到的时候返回提供的默认值,而且还会将键和默认值增加到调用的map中。
返回的是value值

 

 

class Solution {
     public String mostCommonWord(String paragraph, String[] banned) {
        if(paragraph == null || paragraph.length() == 0) return null;
        String newStr = paragraph.toLowerCase();
        Set<Character> set = new HashSet<>();
        set.add('.');
        set.add(',');
        set.add(';');
        set.add('?');
        set.add('!');
        set.add('\'');
        System.out.println(newStr);

        HashMap<String, Integer> map = new HashMap<>();
        String[] str = newStr.split( " " );
        for(int i = 0; i < str.length; i++){
            if(set.contains(str[i].charAt(str[i].length() - 1))){
                str[i] = str[i].substring(0, str[i].length() - 1);
            }
            if(!Arrays.asList( banned ).contains(str[i])){
                if(map.containsKey(str[i])){
                    map.put(str[i], map.get(str[i]) + 1);
                } else{
                    map.put(str[i], 1);
                }
            }
        }

        int max = Integer.MIN_VALUE;
        String res = new String();
        for(Map.Entry<String, Integer> entry : map.entrySet()){
            if(entry.getValue() > max){
                max = entry.getValue();
                res = entry.getKey();
            }
        }
        return res;
    }
}

  

 

优化之后的:

 

class Solution {
     public String mostCommonWord(String paragraph, String[] banned) {
        if(paragraph == null || paragraph.length() == 0) return null;
        String[] str = paragraph.replaceAll("[,.?;'!]", "").toLowerCase().split(" ");
         List<String> newBanned = Arrays.asList(banned);
        HashMap<String, Integer> map = new HashMap<>();
        for(int i = 0; i < str.length; i++){
            if(!newBanned.contains(str[i])){
                map.put(str[i], map.getOrDefault(str[i], 0) + 1);
            }
        }

        String res = null;
        for(String s : map.keySet()){
            if(res == null || map.get(s) > map.get(res)){
                res = s;
            }
        }
        return res;
    }
}

  

posted @ 2018-05-23 16:25  SkyeAngel  阅读(280)  评论(0编辑  收藏  举报