819. Most Common Word

Easy

Problem:

Return the most frequently appearing word, excluding banned words. Do not distinguish between uppercase and lowercase, and also ignore punctuation (such as periods, commas, etc.).

Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation: "hit" occurs 3 times, but it is a banned word. "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. Note that words in the paragraph are not case sensitive, that punctuation is ignored (even if adjacent to words, such as "ball,"), and that "hit" isn't the answer even though it occurs more because it is banned.

https://leetcode.com/problems/most-common-word/arrow-up-right

What to learn:

  • Modifying a list while iterating over it can lead to unpredictable results or bugs. In this case, it's possible that not all instances of a banned word are removed because the iteration skips some elements due to the modification of the list.

for ban in banned:
    for word in result_array:
        if ban.lower() == word:
            result_array.remove(word)
  • In regular expression, \w means Word Character and ^ means not. Therefore, re.sub(r'[^\w]', ' ', paragraph) means to substitute all characters in the paragraph that are not word characters with a space.

  • The python max() function has key= parameter to define a function that returns a value for each element of the iterable.

ages = {
    'Matt': 30,
    'Katie': 29,
    'Nik': 31,
    'Jack': 43,
    'Alison': 32,
    'Kevin': 38
}

# Return the key with the highest value in a Python dictionary.
max_value = max(ages, key=ages.get)
print(max_value)
chevron-rightPython Dictionary Methodshashtag
  • dict() constructs a dictionary.

    • dict(key="value")

    • dict({name: "value"})

    • dict([(name, "value")])

  • len() returns the number of items a dictionary has.

  • get(key) returns the value of the specified key.

  • items() returns a list containing a tuple for each key value pair.

  • keys() returns a list containing the dictionary's keys.

  • values() returns a list of all the values in the dictionary

  • pop(key) removes the element with specified key. We use the del statement to remove an element from the dictionary as well: del dictonary_name["key"]

  • popitem() removes the last inserted key-value pair.

  • has_key(key)returns true if the dictionary contains the specified key. has_key() has been removed in Python 3. in operator is used to check whether a specified key is present or not in a Dictionary.

  • clear() removes all the items from the dictionary.

🙋🏻 How to get key from value in Dictonaryarrow-up-right

  • most_common(n) method of counter class returns a list of top 'n' elements from most common to least common, as specified the parameter 'n'.

Solutions:

  1. Preprocess paragraph by using list comprehension and regular expression.

  2. Return the dictionary key that corresponds with the highest value.

Counter

Defaultdict

chevron-rightdefaultdict(argument)hashtag

It provides a default value for the key that does not exists. Therefore, defaultdict never raises a KeyError.

  • When the list class is passed as the argument, then a defaultdict is created with the values that are list.

  • When the int class is passed as the argument, then a defaultdict is created with default value as zero.

Reference: https://www.geeksforgeeks.org/defaultdict-in-pythonarrow-up-right

Last updated