Skip to content

Commit 2c0f401

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 17.7 MB (73.61%)
1 parent 3518cd1 commit 2c0f401

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>Given a string <code>paragraph</code> and a string array of the banned words <code>banned</code>, return <em>the most frequent word that is not banned</em>. It is <strong>guaranteed</strong> there is <strong>at least one word</strong> that is not banned, and that the answer is <strong>unique</strong>.</p>
2+
3+
<p>The words in <code>paragraph</code> are <strong>case-insensitive</strong> and the answer should be returned in <strong>lowercase</strong>.</p>
4+
5+
<p><strong>Note</strong> that words can not contain punctuation symbols.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> paragraph = &quot;Bob hit a ball, the hit BALL flew far after it was hit.&quot;, banned = [&quot;hit&quot;]
12+
<strong>Output:</strong> &quot;ball&quot;
13+
<strong>Explanation:</strong>
14+
&quot;hit&quot; occurs 3 times, but it is a banned word.
15+
&quot;ball&quot; occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
16+
Note that words in the paragraph are not case sensitive,
17+
that punctuation is ignored (even if adjacent to words, such as &quot;ball,&quot;),
18+
and that &quot;hit&quot; isn&#39;t the answer even though it occurs more because it is banned.
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> paragraph = &quot;a.&quot;, banned = []
25+
<strong>Output:</strong> &quot;a&quot;
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= paragraph.length &lt;= 1000</code></li>
33+
<li>paragraph consists of English letters, space <code>&#39; &#39;</code>, or one of the symbols: <code>&quot;!?&#39;,;.&quot;</code>.</li>
34+
<li><code>0 &lt;= banned.length &lt;= 100</code></li>
35+
<li><code>1 &lt;= banned[i].length &lt;= 10</code></li>
36+
<li><code>banned[i]</code> consists of only lowercase English letters.</li>
37+
</ul>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
3+
banned_Set = set(banned)
4+
count = Counter(re.findall("[a-z]+", paragraph.lower()))
5+
return next(word for word, _ in count.most_common() if word not in banned_Set)

0 commit comments

Comments
 (0)