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 |
반응형
'python-algorithm' 카테고리의 다른 글
백준 25630 팰린드롬 소떡소떡 (0) | 2023.02.08 |
---|---|
leetcode 49. Group Anagrams (0) | 2023.02.07 |
leetcode 937. Reorder Data in Log Files (0) | 2023.02.07 |
leetcode 1967. Number of Strings That Appear as Substrings in Word (2) | 2023.02.03 |
백준 27332 11 月 (November) (0) | 2023.02.01 |
댓글