#147EasyDictionaries & Sets· Counting with Dictionaries
Word Frequency Counter
Input format
`text`: a string. `k`: a positive integer.
Output format
A list of `[word, count]` pairs.
Examples
Input: text = "the cat and the hat and the bat", k = 2
Output: [["the",3],["and",2]]
`the` appears 3 times and `and` twice.
Input: text = "Go, go, GO! Stop.", k = 5
Output: [["go",3],["stop",1]]
Case is ignored and punctuation separates words; only two distinct words exist.
Constraints
- 0 <= len(text) <= 10^5
- 1 <= k <= 100
Study first
- #python
- #dictionaries
- #counting
- #sorting
