692. Top K Frequent Words


https://leetcode.com/problems/top-k-frequent-words/


Runtime: 116 ms, faster than 5.20% of Python3 online submissions for Top K Frequent Words.
Memory Usage: 14.4 MB, less than 67.67% of Python3 online submissions for Top K Frequent Words.



```python
class Solution:
    def topKFrequent(self, words: List[str], k: int) -> List[str]:
        #11:21
        count_dict = {}
        for word in words:
            if word not in count_dict:
                count_dict[word] = 1
            else:
                count_dict[word] += 1
        count_dict = list(sorted(count_dict.items(), key=lambda item: (-item[1], item[0])))
        k_th_elements = count_dict[:k]
        return [item[0] for item in k_th_elements]
        #11:28
        #debug until 11:54; "Sort the words with the same frequency by their lexicographical order." -> "Only do the sort if two words have same frequency"
```
