Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Top K Frequent Words

XPChallenge Points: 20
levelLevel: Medium

You are given a list of words and a number k.
Your task is to identify the k words that appear the most often in the list.

  • If two words have the same frequency, the word which is alphabetically smaller should come first.

  • The final result must be sorted first by frequency (descending) and then by lexicographical order (ascending).

Return the final list of k words.

Example 1:

Input: words = ["apple","banana","apple","orange","banana","apple"], k = 2

Output: ["apple","banana"]

Explanation:

"apple" appears 3 times, "banana" appears 2 times, and "orange" appears once.

Example 2:

Input: words = ["cat","dog","cat","bird","dog","dog"], k = 2

Output: ["dog","cat"]

Explanation:

"Dog" (3 times) > "Cat" (2 times).

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result