Given an array of strings words and an integer k, return the k most frequent strings.

Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.

&nbsp;
Example 1:

Input: words = ["i","love","leetcode","i","love","coding"], k = 2
Output: ["i","love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.


Example 2:

Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output: ["the","is","sunny","day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.


&nbsp;
Constraints:


	1 &lt;= words.length &lt;= 500
	1 &lt;= words[i].length &lt;= 10
	words[i] consists of lowercase English letters.
	k is in the range [1, The number of unique words[i]]


&nbsp;
Follow-up: Could you solve it in O(n log(k)) time and O(n) extra space?

import heapq
class Solution:
    def topKFrequent(self, words: List[str], k: int) -> List[str]:
        
        li = {}
        for i in words:
            if i in li:
                li[i]+=1
            else:
                li[i]=1
        
        heap = []
        for i in li:
            heap.append([-li[i],i])
        
        heapq.heapify(heap)
        
        ans = []
        for i in range(k):
            ans.append(heapq.heappop(heap)[1])
        
        return ans
class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        Map<String,Integer> map=new LinkedHashMap<>();
        for(String word:words) 
            map.put(word,map.getOrDefault(word,0)+1);
        PriorityQueue<Pair<String,Integer>> queue=new PriorityQueue<>(new Comparator<Pair<String,Integer>>(){
            @Override
            public int compare(Pair<String,Integer> a,Pair<String,Integer> b){
                if(a.getValue()!=b.getValue()) return b.getValue()-a.getValue();
                return a.getKey().compareTo(b.getKey());
            }
        });
        map.forEach((key,val)->{
            queue.add(new Pair(key,val));
        });
        List<String> list=new ArrayList<>();
        while(k>0){
            list.add(queue.poll().getKey());
            k--;
        }
        return list;
    }
}
class Solution {
public:
    bool static comp(pair<string, int> a, pair<string, int> b){
        if(a.second > b.second) return true;
        else if(a.second < b.second) return false;
        else{
            return a.first < b.first;
        }
    }
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string, int> m;
        for(auto i : words){
            m[i]++;
        }
        vector<pair<string, int>> v;
        for(auto i : m){
            v.push_back(i);
        }
        sort(v.begin(), v.end(), comp);
        vector<string> ans;
        for(int i=0; i<k; i++){
            ans.push_back(v[i].first);
        }
        return ans;
    }
};
var topKFrequent = function(words, k) {
    let map=new Map()
    let res=[]
    for(let i of words){
        if(map.has(i)){
            map.set(i,map.get(i)+1)
        }else{
            map.set(i,1)
        }
    }
    
    res=[...map.keys()].sort((a,b)=>{
        if(map.get(a)===map.get(b)){
            return b < a ? 1:-1
        }
        return map.get(b)-map.get(a)
    }).slice(0,k)
    
    return res
};
Top K Frequent Words