-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSolution.py
17 lines (13 loc) ยท 874 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import re
from collections import Counter
from typing import List
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
# ์ ๊ท์ ์์ r์ ๋ถ์ด๋ ์ด์ ๋ ์ด์ค์ผ์ดํ์ด ์ ๋๋ก ๋๋๋ก ํ๊ธฐ ์ํจ์ด๋ค.
# ๋ฌธ์ฅ๋ถํธ๋ ๋ฌด์๋๋ ๊ฒ์ด ์๋๋ผ ๊ตฌ๋ถ์๋ก ์ฐ์ธ๋ค.
# not in ์ ์ฐ๋ฉด ์ํ์ง ์๋ ๊ฒ๋ง True ๊ฐ์ด ๋๋ค.
# [0][0]์ ์ฐ๋ ์ด์ ๋ ๊ฐ์ฅ ์ฒซ๋ฒ์งธ ์ธ๋ฑ์ค์ ๊ฐ์๊ฐ ์๋ key๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํจ์ด๋ค.
return Counter([word for word in re.sub(r'[^\w]', ' ', paragraph.lower()).split()
if word not in banned]).most_common(1)[0][0]
print(Solution.mostCommonWord(None, 'Bob hit a ball, the hit BALL flew far after it was hit.', ['hit']))
print(Solution.mostCommonWord(None, 'a, a, a, a, b,b,b,c, c', ['a']))