ByteByteGo logo
menuProblems List

K Most Frequent Strings

Medium

Find the k most frequently occurring strings in an array, and return them sorted by frequency in descending order. If two strings have the same frequency, sort them in lexicographical order.

Example:

Input: strs = ['go', 'coding', 'byte', 'byte', 'go', 'interview', 'go'], k = 2
Output: ['go', 'byte']

Explanation: The strings "go" and "byte" appear the most frequently, with frequencies of 3 and 2, respectively.

Constraints:

  • k ≤ n, where n denotes the length of the array.

You can practice coding exercises online by logging into bytebytego.com on your laptop.