본문 바로가기
python-algorithm

leetcode 819. Most Common Word

by 무적김두칠 2023. 2. 7.

https://leetcode.com/problems/most-common-word/description/

 

Most Common Word - LeetCode

Most Common Word - Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique. The words in paragra

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
import re
import collections
 
 
class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        words = [word for word in re.sub(r'[^\w]'' ', paragraph).lower().split() if word not in banned]
 
        counts = collections.Counter(words)
        return counts.most_common(1)[0][0]
 
cs
반응형

댓글