-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
30 lines (26 loc) · 872 Bytes
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution(object):
def mostCommonWord(self, paragraph, banned):
"""
:type paragraph: str
:type banned: List[str]
:rtype: str
"""
words_map = {}
words = []
paragraph = paragraph.lower()
paragraph = re.sub(r"[!?',;.]", '', paragraph)
for i in range(len(banned)):
banned[i] = banned[i].lower()
for word in paragraph.split(' '):
if words_map.get(word) == None:
words_map[word] = 1
words.append(word)
else:
words_map[word] += 1
res = None
max_count = 0
for word in words:
if word not in banned and words_map[word] > max_count:
res = word
max_count = words_map[word]
return res