819. Most Common Word

原创 2018年04月15日 22:06:42

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

paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
"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 <= paragraph.length <= 1000.
  • 1 <= banned.length <= 100.
  • 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.


import re
class Solution:
    def mostCommonWord(self, paragraph, banned):
        :type paragraph: str
        :type banned: List[str]
        :rtype: str
        li = re.split(r'[^a-zA-Z]{1,2}', paragraph)
        x = []
        for i in range(len(li)):
            if li[i] != "":
        li_set = list(set(x))
        n = 0
        word = ""
        for i in li_set:
            if i not in banned:
                if x.count(i) > n:
                    n = x.count(i)
                    word = i
        return word

版权声明:转载注明 http://blog.csdn.net/birdreamer/article/details/79954106

819-Most Common Word

Description: Given a paragraph and a list of banned words, return the most frequent word that is no...
  • LaputaFallen
  • LaputaFallen
  • 2018-04-15 18:36:57
  • 35

leetcode 159: Longest Substring with At Most Two Distinct Characters

Total Accepted: 1167 Total Submissions: 3961 Given a string, find the length of the lo...
  • xudli
  • xudli
  • 2015-01-28 04:05:56
  • 2272

[LeetCode]79.Word Search

【题目】 Given a 2D board and a word, find if the word exists in the grid. The word can be constructe...
  • SunnyYoona
  • SunnyYoona
  • 2015-01-30 12:27:43
  • 1819


could not be redeployed because it could not be completely removed inthe undeployment phase. The mos...
  • u013381034
  • u013381034
  • 2014-07-25 21:27:59
  • 319

The Two Most Common Graphics Algorithms

Z-buffer projection rendering This is the most commonly-used algorithm, often accelerated with dedic...
  • alldo
  • alldo
  • 2010-01-17 12:51:00
  • 54

leetcode 583. Delete Operation for Two Strings 问题转换+最长公共子串 + DP动态规划

Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t...
  • JackZhang_123
  • JackZhang_123
  • 2017-12-20 09:16:06
  • 62

leetcode 题解代码整理 26-30题

Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such tha...
  • u011932355
  • u011932355
  • 2015-08-15 14:11:02
  • 306

The Most Common but Most Imporant Words

The Most CommonbutMost Imporant Words你是不是生活的哲人?你有勇气微笑的面对人生的每一个挫折,小到别人无意的讥讽?你有勇气倘然的面对人生吗?看看底下这十句西方人的处...
  • Natureboy520
  • Natureboy520
  • 2005-10-05 22:03:00
  • 1298


Matrix and Array Operations To transpose a matrix, use a single quote ('): a' ans = ...
  • sinat_25326461
  • sinat_25326461
  • 2017-02-28 18:52:30
  • 158

LeetCode Array专题

我们工作面试和提高自身数据结构和算法能力的时候往往需要刷刷题,我选择LeetCode是通过一个留学论坛了解的。专业,覆盖语种全面。提前说说刷题的心得: 尽量手写代码,少使用IDE的代码补全和智能提示。...
  • zxccxzzxz
  • zxccxzzxz
  • 2017-01-02 23:13:34
  • 956
您举报文章:819. Most Common Word