692. Top K Frequent Words Medium
1/**
2 * [692] Top K Frequent Words
3 *
4 * Given an array of strings words and an integer k, return the k most frequent strings.
5 * Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
6 *
7 * Example 1:
8 *
9 * Input: words = ["i","love","leetcode","i","love","coding"], k = 2
10 * Output: ["i","love"]
11 * Explanation: "i" and "love" are the two most frequent words.
12 * Note that "i" comes before "love" due to a lower alphabetical order.
13 *
14 * Example 2:
15 *
16 * Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
17 * Output: ["the","is","sunny","day"]
18 * Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
19 *
20 *
21 * Constraints:
22 *
23 * 1 <= words.length <= 500
24 * 1 <= words[i].length <= 10
25 * words[i] consists of lowercase English letters.
26 * k is in the range [1, The number of unique words[i]]
27 *
28 *
29 * Follow-up: Could you solve it in O(n log(k)) time and O(n) extra space?
30 *
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/top-k-frequent-words/
35// discuss: https://leetcode.com/problems/top-k-frequent-words/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40 pub fn top_k_frequent(words: Vec<String>, k: i32) -> Vec<String> {
41 vec![]
42 }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn test_692() {
53 }
54}
55
Back
© 2025 bowen.ge All Rights Reserved.