Skip to content
RED SAGSkills

Most Common Allowed Word

A *word* in paragraph is a maximal run of English letters; everything else (spaces, punctuation, digits, underscores) separates words. Words are compared case-insensitively.

Ignoring the words listed in banned (given in lowercase), return the most frequent word in lowercase. If several words tie, return the alphabetically smallest. At least one allowed word is guaranteed.

Example 1

Input: paragraph = "Deploy the build. The build failed, so redeploy the BUILD!", banned = ["the"]
Output: "build"
Explanation: "build" appears 3 times; "the" also appears 3 times but is banned.

Example 2

Input: paragraph = "a, b, b; A c c", banned = []
Output: "a"
Explanation: a, b and c each appear twice; "a" is smallest.

Constraints

  • 1 <= paragraph.length <= 10^5
  • 0 <= banned.length <= 100
Hint 1

Lowercase the paragraph and pull out runs of letters with a regular expression.

Hint 2

Count in a map, then pick the highest count, breaking ties alphabetically.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Data Scientist, AI Engineer (LLM & Generative AI)

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.