Most Frequent Word

EasyHash Map≈ 20 min

You are given a text and a list of banned words. Return the word the text uses most often that is not banned, written in lowercase.

  • A word is a run of the letters a–z, in any case: "The" and "the" are the same word. Every other character (spaces, digits, punctuation, apostrophes) only separates words, so "it's" is the two words it and s.
  • The banned words are written in lowercase.
  • If two or more words share the highest count, return the one that appears first in the text.
  • If the text has no word that is not banned, return an empty string "".

Examples

text
text = "The cat saw the dog. The dog ran!", banned = ["the"]   →  "dog"
text = "Red, blue; red? BLUE... green", banned = []            →  "red"
text = "to be or not to be", banned = ["to", "be", "or", "not"] →  ""
text = "one", banned = []                                      →  "one"

In the first example the appears three times but is banned; dog appears twice. In the second, red and blue both appear twice, and red comes first.

Constraints

  • The text holds between 0 and 100,000 characters, all of them ASCII (letters, digits, spaces and punctuation); up to 100 banned words.
  • Aim for O(n) in the length of the text.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.