1
+ """
2
+ https://leetcode.com/problems/most-common-word/
3
+
4
+ Strat:
5
+ This is what the solution calls a "pipeline" lol. Essentially taking the
6
+ problem into steps: preprocess the punctuations and spaces, and then use
7
+ a dictionary to store the occurence of each word.
8
+
9
+ Stats: O(n + m) time, O(n + m) space where n is len(paragraph), m is len(banned)
10
+ ^ have to iterate through both arrays, and both use a set or dict
11
+ Runtime: 12 ms, faster than 99.75% of Python online submissions for Most Common Word.
12
+ Memory Usage: 12.7 MB, less than 58.35% of Python online submissions for Most Common Word.
13
+
14
+ """
15
+ class Solution (object ):
16
+ def mostCommonWord (self , paragraph , banned ):
17
+ """
18
+ :type paragraph: str
19
+ :type banned: List[str]
20
+ :rtype: str
21
+ """
22
+ #bring to lowercase
23
+ paragraph = paragraph .lower ()
24
+
25
+ #preprocess to get rid of punctuation symbols (!?',;.)
26
+ symbols = "!?',;."
27
+ for symbol in symbols :
28
+ paragraph = paragraph .replace (symbol , " " )
29
+
30
+ banned_words = set (banned )
31
+
32
+ counts = {}
33
+ words = paragraph .split ()
34
+ for word in words :
35
+ if word not in banned_words :
36
+ counts [word ] = counts .get (word , 0 ) + 1
37
+
38
+ ##using max instead of sort reduces runtime from nlgn to n
39
+ # return sorted(counts.items(), key=lambda x: x[1], reverse=True)[0][0]
40
+ max_pair = max (counts .items (), key = lambda x : x [1 ])
41
+ return max_pair [0 ]
0 commit comments