-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtop-k-frequent-words.js
More file actions
37 lines (30 loc) · 961 Bytes
/
top-k-frequent-words.js
File metadata and controls
37 lines (30 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Problem: Top K Frequent Words
* Link: https://leetcode.com/problems/top-k-frequent-words/
* Difficulty: Medium
*
* Return k most frequent words sorted by frequency (ties: alphabetical).
*
* Time Complexity: O(n log n)
* Space Complexity: O(n)
*/
// JavaScript Solution
function topKFrequent(words, k) {
const freq = new Map();
for (const w of words) freq.set(w, (freq.get(w) || 0) + 1);
// Sort: by frequency desc, then alphabetically for ties
return [...freq.keys()]
.sort((a, b) => {
if (freq.get(b) !== freq.get(a)) return freq.get(b) - freq.get(a);
return a.localeCompare(b); // alphabetical for ties
})
.slice(0, k);
}
module.exports = topKFrequent;
/* Python Solution:
from collections import Counter
def topKFrequent(words, k):
freq = Counter(words)
# Sort by (-frequency, word) so higher freq comes first, ties alphabetical
return sorted(freq.keys(), key=lambda w: (-freq[w], w))[:k]
*/