<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;i&quot;,&quot;love&quot;,&quot;leetcode&quot;,&quot;i&quot;,&quot;love&quot;,&quot;coding&quot;], k = 2
<strong>Output:</strong> [&quot;i&quot;,&quot;love&quot;]
<strong>Explanation:</strong> &quot;i&quot; and &quot;love&quot; are the two most frequent words.
Note that &quot;i&quot; comes before &quot;love&quot; due to a lower alphabetical order.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;the&quot;,&quot;day&quot;,&quot;is&quot;,&quot;sunny&quot;,&quot;the&quot;,&quot;the&quot;,&quot;the&quot;,&quot;sunny&quot;,&quot;is&quot;,&quot;is&quot;], k = 4
<strong>Output:</strong> [&quot;the&quot;,&quot;is&quot;,&quot;sunny&quot;,&quot;day&quot;]
<strong>Explanation:</strong> &quot;the&quot;, &quot;is&quot;, &quot;sunny&quot; and &quot;day&quot; are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 500</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 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 <strong>unique</strong> words[i]]</code></li>
</ul>

<p>&nbsp;</p>
<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>

class Solution {
public:

    void generate_h (string p, vector<int> &v1) { 
        int i = 1 , j = 0 ;
        v1.push_back(-1) ;
        while( i < p.size() ) { 
        
        if ( p[i] != p[j]) { 
            
            v1.push_back(j) ;
            
            while ( j >= 0 && p[i] != p[j]) { 
                j = v1[j] ;
            }
        }
        
        else { 
            v1.push_back(v1[j]) ;
        }
        
        i++ ; j++ ;

        }
        v1.push_back(j) ;
        }

    

    int kmp ( string s , string p ) {
        int i = 0, j = 0 ;  
        int num = 0 ;
        vector<int> h ;
        generate_h(p , h) ;
        while ( i < s.size()) { 
            if ( p[j] == s[i]) { 
                i++ ; j++ ;
                if ( j == p.size()) { 
                num++ ;
                j = h[j] ;
                }
            }
            else {
                j = h[j] ;
                if ( j < 0 ) { 
                    i++ ; j++ ;
                }
            }
        }
        return num ;
    }


    

    class Compare {
    public:
       bool operator()(const pair<string,int> a, const pair<string,int> b) const {
        if ( a.second == b.second) { 
           return a.first > b.first ;
        }
            return a.second < b.second ;
      }
    };



    vector<string> topKFrequent(vector<string>& words, int k) {
    map<string , int> m1 ;

    for ( string k : words ) {
        m1[k] += 1 ;
    }
    set<pair<string ,int> , Compare > pq ;

    for ( auto it = m1.begin() ; it != m1.end() ; it++) { 
        pq.insert( make_pair( it->first , it->second)) ;
    }


    vector<string> v1 ;
    for ( int i = 0 ; i<k ; i++)  { 
        v1.push_back( pq.rbegin()->first ) ;
        pq.erase(prev(pq.end()));
    }
    return v1 ; 


     }
     
    

    
};
18500