struct Group
{
char name;
vector<int>rank;
Group(char n)
{
name = n;
rank = vector<int>(26,0);
}
Group(){}
bool operator < (Group &another)
{
for(int i = 0; i < 26; i++)
{
if(rank[i] > another.rank[i])
{
return true;
}
else if(rank[i] < another.rank[i])
{
return false;
}
}
return name < another.name;
}
};
class Solution {
public:
string rankTeams(vector<string>& votes) {
int n = votes.size(), m = votes[0].size();
unordered_map<char,Group>hash;
for(int i =0; i < m; i++)
{
Group t(votes[0][i]);
hash.insert(make_pair(votes[0][i], t));
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
hash[votes[i][j]].rank[j]++;
}
}
vector<Group>groups;
for(int i = 0; i < m; i++)
{
Group t = hash[votes[0][i]];
groups.push_back(t);
}
sort(groups.begin(), groups.end());
string res = "";
for(int i = 0; i < m; i++)
{
res += groups[i].name;
}
return res;
}
};