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.
Example 1
Input: words = ["i","love","python","i","love","coding"], k = 2
Output: ["i","love"]
Explanation: "i" and "love" each appear twice, more than any other word. They tie on frequency, so they are listed alphabetically: "i" before "love".
Example 2
Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output: ["the","is","sunny","day"]
Explanation: "the" appears 4 times, "is" 3 times, "sunny" 2 times and "day" once. Those are the top 4, listed from most to least frequent.
Example 3
Input: words = ["a"], k = 1
Output: ["a"]
Explanation: The only word is the most frequent one.
Count word frequencies, then order candidates by frequency descending and, for ties, lexicographically ascending; take the first k.
Run checks all cases above. Submit evaluates all test cases.