An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
November 6, 2022 07:32 am GMT
Original Link: https://dev.to/salahelhossiny/most-common-word-26k
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 paragraph are case-insensitive and the answer should be returned in lowercase.
import reclass Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: #List words in paragraph, replacing punctuation with ' ' and all lower case paragraph = re.subn("[.,!?;']", ' ', paragraph.lower())[0].split(' ') #Remove any '' or words in banned from paragraph list paragraph = list(filter(lambda x: x not in banned + [''], paragraph)) #Return most common word in filtered list return Counter(paragraph).most_common(1)[0][0]
Original Link: https://dev.to/salahelhossiny/most-common-word-26k
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To