1class Solution {2public:3bool static comp(pair<string, int> a, pair<string, int> b) {4if (a.second > b.second)5return true;6else if (a.second < b.second)7return false;8else {9return a.first < b.first;10}11}12vector<string> topKFrequent(vector<string> &words, int k) {13unordered_map<string, int> m;14for (auto i : words) {15m[i]++;16}17vector<pair<string, int>> v;18for (auto i : m) {19v.push_back(i);20}21sort(v.begin(), v.end(), comp);22vector<string> ans;23for (int i = 0; i < k; i++) {24ans.push_back(v[i].first);25}26return ans;27}28};