← 返回 meta 的题目列表TopK变体问题
类型:online_judge
meta
Given a list of words in a text, return the top K frequent words. Words should be sorted by frequency in descending order. If two words have the same frequency, then the words should be sorted by alphabetical order. Implement a function to achieve this. The function signature is:
from typing import List
def topKFrequent(words: List[str], k: int) -> List[str]:
pass
Example Input and Output:
Input: words = ["i", "love", "leetcode", "i", "love", "coding"] k = 2
Output: ["i", "love"]
Constraints:
The number of words in words is within the range [1, 10^4].
Each word's length is within the range [1, 10].
k is a positive integer and does not exceed the number of words.
Example
Input
i love leetcode i love coding
2