Class Solution

java.lang.Object
g0601_0700.s0692_top_k_frequent_words.Solution

public class Solution extends Object
692 - Top K Frequent Words.<p>Medium</p> <p>Given an array of strings <code>words</code> and an integer <code>k</code>, return <em>the</em> <code>k</code> <em>most frequent strings</em>.</p> <p>Return the answer <strong>sorted</strong> by <strong>the frequency</strong> from highest to lowest. Sort the words with the same frequency by their <strong>lexicographical order</strong>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;i&rdquo;,&ldquo;love&rdquo;,&ldquo;leetcode&rdquo;,&ldquo;i&rdquo;,&ldquo;love&rdquo;,&ldquo;coding&rdquo;], k = 2</p> <p><strong>Output:</strong> [&ldquo;i&rdquo;,&ldquo;love&rdquo;]</p> <p><strong>Explanation:</strong> &ldquo;i&rdquo; and &ldquo;love&rdquo; are the two most frequent words. Note that &ldquo;i&rdquo; comes before &ldquo;love&rdquo; due to a lower alphabetical order.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;the&rdquo;,&ldquo;day&rdquo;,&ldquo;is&rdquo;,&ldquo;sunny&rdquo;,&ldquo;the&rdquo;,&ldquo;the&rdquo;,&ldquo;the&rdquo;,&ldquo;sunny&rdquo;,&ldquo;is&rdquo;,&ldquo;is&rdquo;], k = 4</p> <p><strong>Output:</strong> [&ldquo;the&rdquo;,&ldquo;is&rdquo;,&ldquo;sunny&rdquo;,&ldquo;day&rdquo;]</p> <p><strong>Explanation:</strong> &ldquo;the&rdquo;, &ldquo;is&rdquo;, &ldquo;sunny&rdquo; and &ldquo;day&rdquo; are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 500</code></li> <li><code>1 <= words[i] <= 10</code></li> <li><code>words[i]</code> consists of lowercase English letters.</li> <li><code>k</code> is in the range <code>[1, The number of **unique** words[i]]</code></li> </ul> <p><strong>Follow-up:</strong> Could you solve it in <code>O(n log(k))</code> time and <code>O(n)</code> extra space?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details