leetcode/rankTeams.cpp

36 lines
808 B
C++
Raw Permalink Normal View History

2025-01-02 10:57:27 +00:00
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
class Solution {
public:
string rankTeams(vector<string>& votes) {
unordered_map<char,int> list;
for(auto i : votes){
for(int j = 0;j < i.size();j++){
list[i[j]] += i.size() - j;
}
}
string res;
vector<pair<char,int>> temp(list.begin(),list.end());
stable_sort(temp.begin(),temp.end(),[](pair<char,int> a,pair<char,int> b){
return a.second > b.second;
});
for(auto i : temp){
res += i.first;
}
return res;
}
};
int main(){
vector<string> votes = {"WXYZ","XYZW"};
Solution s;
cout << s.rankTeams(votes) << endl;
return 0;
}