MostCommonWord [source code]


public class MostCommonWord {
static
/******************************************************************************/
class Solution {
    public String mostCommonWord(String paragraph, String[] banned) {
        paragraph = paragraph.toLowerCase ().replaceAll ("[^a-zA-Z ]", "");
        String[] tokens = paragraph.split ("\\s+");
        Set<String> banned_set = new HashSet<> ();
        for (String w : banned)
            banned_set.add (w);
        Map<String, Integer> counts = new HashMap<> ();
        for (String word : tokens) {
            if (banned_set.contains (word))
                continue;
            counts.put (word, counts.getOrDefault (word, 0) + 1);
        }
        String max_word = "";
        int max_count = 0;
        for (String word : counts.keySet ()) {
            int count = counts.get (word);
            if (count > max_count) {
                max_count = count;
                max_word = word;
            }
        }
        return max_word;
    }
}
/******************************************************************************/

    public static void main(String[] args) {
        MostCommonWord.Solution tester = new MostCommonWord.Solution();
    }
}

轻松解决;

UNFINISHED


uwi:

class Solution {  
    public String mostCommonWord(String paragraph, String[] banned) {  
        String[] sp = paragraph.trim().split(" +");  
        Map<String, Integer> ct = new HashMap<>();  
        for(String s : sp){  
            s = s.toLowerCase();  
            while(s.length() > 0 && !(  
                    s.charAt(s.length()-1) >= 'a' &&  
                    s.charAt(s.length()-1) <= 'z')){  
                s = s.substring(0, s.length()-1);  
            }  
            if(s.length() == 0)continue;  
            if(!ct.containsKey(s)){  
                ct.put(s, 0);  
            }  
            ct.put(s, ct.get(s) + 1);  
        }  
        for(String b : banned){  
            ct.remove(b);  
        }  
        int max = -1;  
        String best = null;  
        for(String k : ct.keySet()){  
            if(ct.get(k) > max){  
                max = ct.get(k);  
                best = k;  
            }  
        }  
        return best;  
    }  
}

反正就是这么个题吧.

cchao:

from collections import Counter  

class Solution(object):  
    def mostCommonWord(self, s, banned):  
        """  
        :type paragraph: str  
        :type banned: List[str]  
        :rtype: str  
        """  
        s = map(lambda x : x.lower(), s)  
        s = map(lambda x : x if x.islower() else ' ', s)  
        s = ''.join(s)  
        s = s.split()  
        s = filter(lambda x : x not in banned, s)  
        cnt = Counter(s)  
        return cnt.most_common(1)[0][0]

Python的做法, 了解一下;


Problem Description

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.

Note:

  • 1 <= paragraph.length <= 1000.
  • 1 <= banned.length <= 100.
  • 1 <= banned[i].length <= 10.
  • The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
  • paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
  • Different words in paragraph are always separated by a space.
  • There are no hyphens or hyphenated words.
  • Words only consist of letters, never apostrophes or other punctuation symbols.

Difficulty:Easy
Total Accepted:1.6K
Total Submissions:3K
Contributor:awice
Companies
amazon
Related Topics
string

results matching ""

    No results matching ""