819. 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.






Regular expression 

http://www.vogella.com/tutorials/JavaRegularExpressions/article.html



    
\w    A word character, short for [a-zA-Z_0-9]
\W    A non-word character [^\w]


\W matches a non-word character
+ matches 1 or more occurrences of the subpattern this quantifier modifies.




Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet())
{
    System.out.println(entry.getKey() + "/" + entry.getValue());






class Solution {
    public String mostCommonWord(String p, String[] banned) {
        // use a set to store all the banned words 
        // traverse the string and , every time we see a word, if it's not in the banned words set, update it's freq
        // the map, key is each word, in lower case 
        // one way : Set<String> ban = new HashSet<>(Arrays.asList(banned));
        Set<String> ban = new HashSet<>();
        for(String word : banned) ban.add(word);
        Map<String, Integer> map = new HashMap<>();
        // use regular expression to get all strings in lowercase 
        // don't know how this works 
        String[] words = p.replaceAll("\\W+" , " ").toLowerCase().split("\\s+");
        for(String word : words){
            if(!ban.contains(word)){
                map.put(word, map.getOrDefault(word, 0) + 1);
            }
        }
        int max = 0;
        String res = "";
        
        // traverse the map
        for(Map.Entry<String, Integer> entry : map.entrySet()){
            if(entry.getValue() > max){
                max = entry.getValue();
                res = entry.getKey();
            }
        }
        return res;
    }
}
        
        


 // public String mostCommonWord(String p, String[] banned) {
 //        Set<String> ban = new HashSet<>(Arrays.asList(banned));
 //        Map<String, Integer> count = new HashMap<>();
 //        String[] words = p.replaceAll("\\W+" , " ").toLowerCase().split("\\s+");
 //        for (String w : words) if (!ban.contains(w)) count.put(w, count.getOrDefault(w, 0) + 1);
 //        return Collections.max(count.entrySet(), Map.Entry.comparingByValue()).getKey();
 //    }

 

posted on 2018-11-09 06:37  猪猪&#128055;  阅读(100)  评论(0)    收藏  举报

导航