문제이동
난이도 : ⭐️⭐️
from collections import Counter
import re
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
words = re.sub('[^\w]', ' ', paragraph).lower().split()
for word, count in Counter(words).most_common():
if word in banned:
continue
return word
Runtime : 36 ms
Memory : 13.9 MB
from collections import Counter
import re
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
words = [word for word in re.sub('[^\w]', ' ', paragraph).lower().split() if word not in banned]
return Counter(words).most_common(1)[0][0]
Runtime : 85 ms
Memory : 13.7 MB
첫 번째 제출이 for문을 끝까지 안 돌아서 더 빠른건가
문자가 아닌 것들을 모두 공백으로 처리하면 특수기호들을 제거할 수 있다.
문자를 의미하는 regexp: \w
특수기호 지우는 법 : re.sub('[^\w]', ' ', paragraph)
아마도?