> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/heaps-and-priority-queue/692.-top-k-frequent-words.md).

# 692. Top K Frequent Words

## Problem Statement

\
Given an array of strings `words` and an integer `k`, return *the* `k` *most frequent strings*.

Return the answer **sorted** by **the frequency** from highest to lowest. Sort the words with the same frequency by their **lexicographical order**.

&#x20;

**Example 1:**

<pre><code><strong>Input: words = ["i","love","leetcode","i","love","coding"], k = 2
</strong><strong>Output: ["i","love"]
</strong><strong>Explanation: "i" and "love" are the two most frequent words.
</strong>Note that "i" comes before "love" due to a lower alphabetical order.
</code></pre>

**Example 2:**

<pre><code><strong>Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
</strong><strong>Output: ["the","is","sunny","day"]
</strong><strong>Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= words.length <= 500`
* `1 <= words[i].length <= 10`
* `words[i]` consists of lowercase English letters.
* `k` is in the range `[1, The number of`` `**`unique`**` ``words[i]]`

&#x20;

**Follow-up:** Could you solve it in `O(n log(k))` time and `O(n)` extra space?

## Intuition

```
Approach:
Custom Comparator
```

### Links

<https://leetcode.com/problems/top-k-frequent-words/description/>

### Video Links

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
typedef pair<int, string> pis;

struct Comp {
    bool operator()(const pair<int, string>& p1, const pair<int, string>& p2) {
        // Compare integers in descending order
        if (p1.first != p2.first) {
            return p1.first < p2.first; // Change to > for ascending order
        }
        // If integers are equal, compare strings in ascending order
        return p1.second > p2.second; // Change to < for descending order
    }
};

class Solution {
public:
    vector<string> topKFrequent(vector<string>& arr, int k) {
        vector<string> ans;
        unordered_map<string, int> mp;
        priority_queue<pis, vector<pis>, Comp> pq;
        for(auto &it: arr)
            mp[it]++;

        for(auto &it: mp)
            pq.push({it.second, it.first});

        while(k--){
            ans.push_back(pq.top().second);
            pq.pop();
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
