Top k frequent words

Write codeEasy20 min

Your task

Given a block of text, a list of stopwords, and an integer k, return the k most frequent words. Words are maximal runs of alphabetic characters, compared case-insensitively (lowercase everything). Discard any word in the stopword set. Break ties between equally frequent words alphabetically (ascending). Return at most k words as a list, most frequent first.

Examples

top_k_words(text: str, stopwords: list[str], k: int) -> list[str]
Example 1
Inputtop_k_words("the cat sat on the mat the cat", ["the", "on"], 2)
Output["cat", "mat"]