↓Skip to main content
  1. LeetCode/

LeetCode 819: Most Common Word

·2 mins· ·
LeetCode Easy Array Hash-Table String Counting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-14

  • Submissions: Wrong Answer ×2 → Runtime Error → Wrong Answer → Accepted ×2 → Wrong Answer → Accepted

Approach
#

Normalize first: replace non-letters with spaces (deleting them would glue a,b into ab), lowercase, and split() (String Template 4). Put banned in a set, skip it while counting (Hash Table Template 1), and take the most frequent.

My Attempts
#

This took 8 submissions, each missing one normalization detail:

  • Attempt 1 used paragraph[:-1] to drop trailing punctuation, but there may be none: "a" became an empty string.
  • Attempt 2 deleted punctuation: "a'a" became "aa", while the answer is "a".
  • After switching to spaces, one version had a misspelled variable (NameError) and two forgot to lowercase: "Bob" returned "Bob" instead of "bob".

Solution
#

class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        words = "".join(c.lower() if c.isalpha() else " " for c in paragraph).split()
        ban = set(banned)
        cnt = Counter(w for w in words if w not in ban)
        return cnt.most_common(1)[0][0]

Complexity
#

Time O(n + b), space O(n + b).

Mistakes to Avoid
#

  • Replace punctuation with spaces rather than deleting it.
  • Matching is case-insensitive; lowercase everything.
  • Do not drop the last character: the paragraph need not end with punctuation.

Interview Explanation
#

I turn non-letters into spaces, lowercase, split, skip banned words via a set while counting, and return the most frequent word. Time O(n + b).

Related

LeetCode 748: Shortest Completing Word
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 748 note: count the plate’s letters in lowercase, then find the shortest word covering every count, taking the earliest on ties
LeetCode 599: Minimum Index Sum of Two Lists
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 599 note: map list1’s strings to indices, sum both indices while scanning list2, and keep every string with the minimum sum
LeetCode 383: Ransom Note
·1 min
LeetCode Easy Hash-Table String Counting
LeetCode 383 note: every character’s demand in ransomNote must not exceed its supply in magazine; an empty Counter difference means yes
LeetCode 387: First Unique Character in a String
·1 min
LeetCode Easy Hash-Table String Queue Counting
LeetCode 387 note: count first, then scan left to right for the first character with count 1
LeetCode 804: Unique Morse Code Words
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 804 note: translate each word into a Morse string and count the distinct results with a set
LeetCode 500: Keyboard Row
·1 min
LeetCode Easy Array Hash-Table String
LeetCode 500 note: make a set for each keyboard row and keep words whose lowercase character set is a subset of one row