Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

137. Top K Frequent Words

MediumDay 41 of 60Heap / Priority Queue

Given an array of strings words and an integer k, return the k most frequent words. Order the result from highest frequency to lowest, and break ties by putting the alphabetically smaller word first.

Example 1
Input: words = ["i","love","leetcode","i","love","coding"], k = 2
Output: ["i","love"]
"i" and "love" both appear twice, and "i" comes first alphabetically.
Example 2
Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output: ["the","is","sunny","day"]
Frequencies are 4, 3, 2 and 1.
Constraints
  • 1 <= words.length <= 500
  • 1 <= words[i].length <= 10
  • words[i] has only lowercase letters
  • 1 <= k <= number of distinct words
Hints
Frequently asked at: Amazon, Uber, Bloomberg
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min