Skip to content

Practice Top K Frequent Words

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Problem Statement

Return the k most frequent strings in words. Order results by decreasing frequency; break equal-frequency ties by increasing lexicographical order. Follow-up: achieve O(n log(k)) time and O(n) extra space.

Source: Top K Frequent Words on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
words = ["i","love","leetcode","i","love","coding"], k = 2
Output
["i","love"]

# Example 2

Input
words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output
["the","is","sunny","day"]

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]]

Related LeetCode Problems

Practice more problems similar to Top K Frequent Words.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.