Trains the technique from
LeetCode 819Most Common WordThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A notice is given as the string text, made of English letters in either case, spaces, and the marks '!', '?', "'", ',', ';' and '.'. A list of barred words is given as barred, all in lowercase.
Split the notice into words, where a word is a run of letters and everything else is a separator. Words are compared with case ignored.
Return, in lowercase, the word that appears most often among those not barred. Exactly one word appears most often, and at least one word is not barred.
Example 1
Case is ignored, so the three spellings of the same word count together as three, while the barred word is left out.
Example 2
The middle word is barred, leaving the other appearing twice.
Example 3
The first word appears three times against the other's two, and since case is ignored the capitalised spelling counts with the rest.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def most_common_word(text: str, barred: list[str]) -> str:public String mostCommonWord(String text, String[] barred)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.