forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopKFrequentWords.java
More file actions
56 lines (50 loc) · 1.83 KB
/
TopKFrequentWords.java
File metadata and controls
56 lines (50 loc) · 1.83 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.thealgorithms.strings;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Utility class to find the top-k most frequent words.
*
* <p>Words are ranked by frequency in descending order. For equal frequencies,
* words are ranked in lexicographical ascending order.
*
* <p>Reference:
* https://en.wikipedia.org/wiki/Top-k_problem
*
*/
public final class TopKFrequentWords {
private TopKFrequentWords() {
}
/**
* Finds the k most frequent words.
*
* @param words input array of words
* @param k number of words to return
* @return list of top-k words ordered by frequency then lexicographical order
* @throws IllegalArgumentException if words is null, k is negative, or words contains null
*/
public static List<String> findTopKFrequentWords(String[] words, int k) {
if (words == null) {
throw new IllegalArgumentException("Input words array cannot be null.");
}
if (k < 0) {
throw new IllegalArgumentException("k cannot be negative.");
}
if (k == 0 || words.length == 0) {
return List.of();
}
Map<String, Integer> frequency = new HashMap<>();
for (String word : words) {
if (word == null) {
throw new IllegalArgumentException("Input words cannot contain null values.");
}
frequency.put(word, frequency.getOrDefault(word, 0) + 1);
}
List<String> candidates = new ArrayList<>(frequency.keySet());
candidates.sort(Comparator.<String>comparingInt(frequency::get).reversed().thenComparing(Comparator.naturalOrder()));
int limit = Math.min(k, candidates.size());
return new ArrayList<>(candidates.subList(0, limit));
}
}