#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
class Solution {
public:
/**
使用哈希表记录次数,然后转成vector,自定义排序,添加前k个
*/
vector<vector<string> > topKstrings(vector<string>& strings, int k) {
if(k==0){
return {};
}
unordered_map<string, int> mp;
for(const string& s:strings){
mp[s]++;
}
vector< pair<string, int> > vec(mp.begin(), mp.end());
sort( vec.begin(), vec.end(), [&](const pair<string, int>& a,
const pair<string, int>& b){
if(a.second == b.second){
return a.first < b.first; //二次排序
}
return a.second>b.second;} ); //return 排序规则;
vector< vector<string> > ans;
for(int i=0; i<k; i++){
ans.push_back( {vec[i].first, to_string(vec[i].second)} );
}
return ans;
}
};