-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopKfrequentWords.java
More file actions
40 lines (35 loc) · 1.8 KB
/
Copy pathTopKfrequentWords.java
File metadata and controls
40 lines (35 loc) · 1.8 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
/*
Top K frequent words : https://leetcode.com/problems/top-k-frequent-words/
*/
import java.util.*;
import java.util.stream.*;
/*
Top K frequent words - https://leetcode.com/problems/top-k-frequent-words/
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
Example 1:
Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.*/
class TopKfrequentWords {
public static List<String> topKFrequent(String[] words, int k) {
Map<String, Integer> countMap = new HashMap();
for (String word : words) {
int count = countMap.getOrDefault(word, 0) + 1;
countMap.put(word, count);
}
//sort the countMap in order of frequency
LinkedHashMap<String, Integer> reverseSortedMap = new LinkedHashMap<>();
Comparator<Map.Entry<String, Integer>> keyComparator = Map.Entry.comparingByKey();
Comparator<Map.Entry<String, Integer>> comparator = Map.Entry.comparingByValue(Comparator.reverseOrder());
countMap.entrySet().stream().sorted(comparator.thenComparing(keyComparator))
.forEachOrdered(x -> reverseSortedMap.put(x.getKey(), x.getValue()));
List<String> values = reverseSortedMap.entrySet().stream().map(x -> x.getKey()).collect(Collectors.toList());
return values.subList(0, k);
}
public static void main(String[] args) {
String[] input = new String[] {"i", "love", "leetcode", "i", "love", "coding"};
System.out.println(topKFrequent(input, 2));
}
}