Rank Teams by Votes

Problem Id: 1366 Difficulty: Medium Tag: Array Tag: Sort


We need to sort all the numbers by its weighted frequency. That is to say, for example, if we use a list to save the counts of a letter in each position, we would get the result by sorting it in the following way,:

[1, 1] > [1, 0]
[2, 1] > [1, 2]

This is the same as the rule how python sort the list. So we could use python's sort to sort it.


import string

class Solution:
    def rankTeams(self, votes: List[str]) -> str:
        score = {c: ([0] * 26 + [-ord(c)]) for c in string.ascii_uppercase}
        for vote in votes:
            for i, c in enumerate(vote):
                score[c][i] += 1
        letters = list(set(votes[0]))
        letters.sort(key=lambda c: score[c], reverse=True)
        return ''.join(letters)