Top K Frequent Words
1 approach, code in every language
Problem description
You are given a list of words and an integer k. Return the k words that appear most frequently.
Order the answer by frequency from highest to lowest. When two words occur the same number of times, place the lexicographically smaller word first.
Example 1
Input: words = ["i","love","leetcode","i","love","coding"], k = 2
Output: ["i","love"]
"i" and "love" both appear twice; the others appear once, so the two most frequent words are returned.
Example 2
Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output: ["the","is","sunny","day"]
Counts are the:4, is:3, sunny:2, day:1, so they are ordered by decreasing frequency.
Constraints
1 <= words.length <= 5001 <= words[i].length <= 10words[i] consists of lowercase English lettersk is in the range [1, the number of unique words]Words with the same frequency are ordered lexicographically
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Most Drawn Part Codes is our own problem on the technique behind Top K Frequent Words: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free