Top K Frequent Words

interview_workbook/leetcode/heap /app/src/interview_workbook/leetcode/heap/top_k_frequent_words.py
View Source

Algorithm Notes

Summary: Top K Frequent Words — notes not yet curated.
Time: Estimate via loops/recurrences; common classes: O(1), O(log n), O(n), O(n log n), O(n^2)
Space: Count auxiliary structures and recursion depth.
Tip: See the Big-O Guide for how to derive bounds and compare trade-offs.

Big-O Guide

Source

"""
Top K Frequent Words

TODO: Add problem description
"""

import heapq
import random
from collections import Counter

from src.interview_workbook.leetcode._registry import register_problem
from src.interview_workbook.leetcode._types import Category, Difficulty


class Solution:
    def top_k_frequent(self, words: list[str], k: int) -> list[str]:
        """
        Return the k most frequent words sorted by frequency and lexicographic order.
        """
        counts = Counter(words)
        # min-heap with custom ordering
        heap = [(-freq, word) for word, freq in counts.items()]
        heapq.heapify(heap)

        result = []
        for _ in range(k):
            if heap:
                freq, word = heapq.heappop(heap)
                result.append(word)
        return result


def demo() -> str:
    """Demonstration of Top K Frequent Words with deterministic seeding."""
    random.seed(0)
    words = ["i", "love", "leetcode", "i", "love", "coding"]
    k = 2
    sol = Solution()
    result = sol.top_k_frequent(words, k)
    return f"Words: {words}, K: {k}, Top K Frequent: {result}"


register_problem(
    id=692,
    slug="top_k_frequent_words",
    title="Top K Frequent Words",
    category=Category.HEAP,
    difficulty=Difficulty.MEDIUM,
    tags=["string", "hashmap", "heap", "sorting"],
    url="https://leetcode.com/problems/top-k-frequent-words/",
    notes="",
)