LeetCode - The World's Leading Online Programming Learning Platform
문제 요약
import re
from typing import List
from collections import Counter
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
paragraph_alphanumeric = re.sub(r"[^0-9a-zA-Z\s]", " ", paragraph.lower())
exclude_paragraph = [word for word in paragraph_alphanumeric.split() if word not in banned]
return Counter(exclude_paragraph).most_common()[0][0]
re.sub(r"[^0-9a-zA-Z\s]", " ", paragraph.lower())
0-9: 숫자
a-z: 알파벳 소문자
A-Z: 알파벳 대문자
\s: white space (spaces, tabs, line break)
^: not
⇒ paragraph.lower()에서 숫자, 알파벳, white space를 제외한 모든 단어들 제거 (특수문자 제거)
Counter(exclude_paragraph).most_common()[0][0]
List[Tuple(str, int)]
의 형태로 리턴해준다.=> 유의미한 차이는 없었다!
https://docs.python.org/3/library/collections.html#counter-objects