Top K Frequent Words
Given an array of strings words and an integer k, return the k most frequent words. Sort the result by descending frequency; words with the same frequency are ordered lexicographically (alphabetically ascending).
Open official problem prompt ↗Return the k words that appear most often, breaking frequency ties alphabetically, in the correct ranked order.
A song chart ranks tracks by play count. Two tracks tied on plays are listed alphabetically. You count every play, then read off the top k from the chart.
- Input
- words = ["i","love","leetcode","i","love","coding"], k = 2
- Output
- ["i", "love"]
- Why
- "i" and "love" each appear twice (more than any other word); tie broken alphabetically, and both beat the frequency-1 words.
1 <= words.length <= 5001 <= words[i].length <= 10words[i] consists of lowercase English lettersk is in the range [1, number of unique words]