|
3 | 3 | // https://leetcode.com/problems/top-k-frequent-words/description/ |
4 | 4 |
|
5 | 5 | import java.util.*; |
| 6 | +import java.util.stream.Collectors; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * 692. Top K Frequent Words |
@@ -99,5 +100,165 @@ public class TopKFrequentWords { |
99 | 100 | // } |
100 | 101 |
|
101 | 102 | // V1 |
| 103 | + // IDEA: SORT |
| 104 | + // https://leetcode.com/problems/top-k-frequent-words/solutions/1244692/java-solution-by-keerthy0212-1jtv/ |
| 105 | + public List<String> topKFrequent_1(String[] words, int k) { |
| 106 | + HashMap<String, Integer> freq = new HashMap<>(); |
| 107 | + for (int i = 0; i < words.length; i++) { |
| 108 | + freq.put(words[i], freq.getOrDefault(words[i], 0) + 1); |
| 109 | + } |
| 110 | + List<String> res = new ArrayList(freq.keySet()); |
| 111 | + // sorting |
| 112 | + // if two words have the same frequency, then the word with the lower |
| 113 | + // alphabetical order comes first. |
| 114 | + // else most frequent words will come first |
| 115 | + Collections.sort(res, |
| 116 | + (w1, w2) -> freq.get(w1).equals(freq.get(w2)) ? w1.compareTo(w2) : freq.get(w2) - freq.get(w1)); |
| 117 | + |
| 118 | + return res.subList(0, k); |
| 119 | + } |
| 120 | + |
102 | 121 | // V2 |
| 122 | + // IDEA: SORT |
| 123 | + // https://leetcode.com/problems/top-k-frequent-words/solutions/2720232/java-easy-solution-with-explanation-hash-mk80/ |
| 124 | + public List<String> topKFrequent_2(String[] words, int k) { |
| 125 | + |
| 126 | + // map hold the word: counts |
| 127 | + HashMap<String, Integer> map = new HashMap(); |
| 128 | + |
| 129 | + // sort the map by frequency high->low order, sort words lexi order |
| 130 | + PriorityQueue<Map.Entry<String, Integer>> heap = new PriorityQueue<>( |
| 131 | + (a, b) -> { |
| 132 | + if (a.getValue() != b.getValue()) |
| 133 | + return a.getValue().compareTo(b.getValue()); |
| 134 | + return -a.getKey().compareTo(b.getKey()); |
| 135 | + }); |
| 136 | + |
| 137 | + // fill the map |
| 138 | + for (String word : words) { |
| 139 | + map.merge(word, 1, Integer::sum); |
| 140 | + } |
| 141 | + |
| 142 | + // put into heap |
| 143 | + for (Map.Entry<String, Integer> entry : map.entrySet()) { |
| 144 | + heap.offer(entry); |
| 145 | + if (heap.size() > k) |
| 146 | + heap.poll(); |
| 147 | + } |
| 148 | + |
| 149 | + // pop out the answer |
| 150 | + List<String> ans = new ArrayList(); |
| 151 | + while (heap.size() > 0) |
| 152 | + ans.add(heap.poll().getKey()); |
| 153 | + |
| 154 | + // check the order |
| 155 | + Collections.reverse(ans); |
| 156 | + return ans; |
| 157 | + } |
| 158 | + |
| 159 | + // V3 |
| 160 | + // IDEA: SORT + STREAM API |
| 161 | + // https://leetcode.com/problems/top-k-frequent-words/solutions/2721452/java-stream-api-memory-usage-less-than-8-d1t4/ |
| 162 | + public List<String> topKFrequent_3(String[] words, int k) { |
| 163 | + TreeMap<String, Integer> map = new TreeMap<>(String::compareTo); |
| 164 | + Arrays.stream(words).forEach(x -> map.put(x, map.getOrDefault(x, 0) + 1)); |
| 165 | + return map.entrySet().stream() |
| 166 | + .sorted((o1, o2) -> Integer.compare(o2.getValue(), o1.getValue())) |
| 167 | + .map(Map.Entry::getKey) |
| 168 | + .limit(k) |
| 169 | + .collect(Collectors.toList()); |
| 170 | + } |
| 171 | + |
| 172 | + // V4 |
| 173 | + // https://leetcode.com/problems/top-k-frequent-words/solutions/5983153/simple-solution-with-diagrams-in-video-j-myeq/ |
| 174 | + class TrieNode { |
| 175 | + TrieNode[] children; |
| 176 | + String word; |
| 177 | + |
| 178 | + public TrieNode() { |
| 179 | + this.children = new TrieNode[26]; |
| 180 | + this.word = null; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + class Trie { |
| 185 | + private TrieNode root; |
| 186 | + |
| 187 | + public Trie() { |
| 188 | + root = new TrieNode(); |
| 189 | + } |
| 190 | + |
| 191 | + public void addWord(String word) { |
| 192 | + TrieNode cur = root; |
| 193 | + for (char c : word.toCharArray()) { |
| 194 | + int index = c - 'a'; |
| 195 | + if (cur.children[index] == null) { |
| 196 | + cur.children[index] = new TrieNode(); |
| 197 | + } |
| 198 | + cur = cur.children[index]; |
| 199 | + } |
| 200 | + cur.word = word; |
| 201 | + } |
| 202 | + |
| 203 | + public void getWords(TrieNode node, List<String> result) { |
| 204 | + if (node == null) |
| 205 | + return; |
| 206 | + if (node.word != null) |
| 207 | + result.add(node.word); |
| 208 | + for (TrieNode child : node.children) { |
| 209 | + if (child != null) { |
| 210 | + getWords(child, result); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + // Added method to expose root |
| 216 | + public TrieNode getRoot() { |
| 217 | + return root; |
| 218 | + } |
| 219 | + } |
| 220 | + public List<String> topKFrequent_4(String[] words, int k) { |
| 221 | + Map<String, Integer> frequencyMap = new HashMap<>(); |
| 222 | + List<Trie> buckets = new ArrayList<>(Collections.nCopies(words.length + 1, null)); |
| 223 | + List<String> topK = new ArrayList<>(); |
| 224 | + |
| 225 | + // Count word frequencies |
| 226 | + for (String word : words) { |
| 227 | + frequencyMap.put(word, frequencyMap.getOrDefault(word, 0) + 1); |
| 228 | + } |
| 229 | + |
| 230 | + // Add words to buckets based on frequency |
| 231 | + for (Map.Entry<String, Integer> entry : frequencyMap.entrySet()) { |
| 232 | + String word = entry.getKey(); |
| 233 | + int freq = entry.getValue(); |
| 234 | + if (buckets.get(freq) == null) { |
| 235 | + buckets.set(freq, new Trie()); |
| 236 | + } |
| 237 | + buckets.get(freq).addWord(word); |
| 238 | + } |
| 239 | + |
| 240 | + // Retrieve top k words from buckets |
| 241 | + for (int i = buckets.size() - 1; i >= 0 && topK.size() < k; i--) { |
| 242 | + if (buckets.get(i) != null) { |
| 243 | + List<String> wordsInBucket = new ArrayList<>(); |
| 244 | + // Use the new getRoot method |
| 245 | + buckets.get(i).getWords(buckets.get(i).getRoot(), wordsInBucket); |
| 246 | + |
| 247 | + // Sort words in lexicographical order |
| 248 | + Collections.sort(wordsInBucket); |
| 249 | + |
| 250 | + // Add words from the bucket to the result until top k is filled |
| 251 | + for (String word : wordsInBucket) { |
| 252 | + if (topK.size() < k) { |
| 253 | + topK.add(word); |
| 254 | + } else { |
| 255 | + break; |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + return topK; |
| 262 | + } |
| 263 | + |
103 | 264 | } |
0 commit comments