Your question is Python Coding With Efficient Structures. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Apple Music analyzes search terms to identify the most popular queries. Given a list of search terms and an integer k, return the k most frequent terms.
Order the result by decreasing frequency. If two terms have the same frequency, order them lexicographically. Each term should appear only once in the result.
Implement top_k_search_terms(terms, k):
terms, a list of lowercase strings, and k, an integer.k strings ordered by frequency descending, then lexicographically ascending for ties.Example 1:
Input: terms = ["pop", "rock", "pop", "jazz", "rock", "pop"], k = 2
Output: ["pop", "rock"]
pop appears three times and rock appears twice.
Example 2:
Input: terms = ["beta", "alpha", "beta", "alpha", "gamma"], k = 2
Output: ["alpha", "beta"]
alpha and beta both appear twice, so lexicographic order breaks the tie.
def top_k_search_terms(terms, k):