819. Most Common Word
EasyLeetCodeGiven a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.
The words in paragraph are case-insensitive and the answer should be returned in lowercase.
Note that words can not contain punctuation symbols.
Example 1
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.
Example 2
Input: paragraph =
"a.", banned =[]Output:
"a"
Constraints
1 <= paragraph.length <= 1000- paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".
0 <= banned.length <= 1001 <= banned[i].length <= 10- banned[i] consists of only lowercase English letters.
How to solve the problem
- Hash Table with String Processing
Code
import string
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
paragraph = paragraph.lower()
for ch in string.punctuation:
paragraph = paragraph.replace(ch, " ")
words = paragraph.split()
word_count = {}
for word in words:
if word not in banned:
word_count[word] = word_count.get(word, 0) + 1
result = max(word_count, key=word_count.get)
return resultfrom collections import Counter
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
paragraph = paragraph.lower()
for ch in string.punctuation:
paragraph = paragraph.replace(ch, ' ')
paragraph = paragraph.split()
wordsDict = Counter(paragraph)
maxCount = 0
maxWord = ""
for word in wordsDict:
if word not in banned and wordsDict.get(word) > maxCount:
maxCount = wordsDict.get(word)
maxWord = word
return maxWordfrom collections import Counter
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
paragraph = paragraph.lower()
for ch in string.punctuation:
paragraph = paragraph.replace(ch, ' ')
paragraph = paragraph.split()
wordsDict = Counter(paragraph)
# use generator expression
return max((word for word in wordsDict if word not in banned), key=wordsDict.get)Complexity
Approach 1: Hash Table with String Processing
Time complexity: O(n + m)
- Converting paragraph to lowercase: O(n)
- Replacing punctuation: O(n)
- Splitting into words: O(n)
- Counting words: O(n)
- Finding max: O(n)
- Total: O(n + m) where n is paragraph length, m is banned words length
Space complexity: O(n)
- We store all words in the word_count dictionary, which can be up to O(n) in worst case.
Comments
No comments yet. Be the first to comment!