Top K Frequent Words

Given an array of strings, return the k most frequent strings. The result should be sorted by frequency from highest to lowest, and by lexicographical order for words with the same frequency.

Constraints

  • 1 <= words.length <= 500
  • 1 <= words[i].length <= 10
  • words[i] consists of lowercase English letters.
  • k is in the range [1, The number of unique words[i]]
Saving… · 2 lines
class Solution:
def topKFrequentWords(self, words: List[str], k: int) -> List[str]:
Console

Click Run to execute sample tests.