1
# ["ABC","ACB","ABC","ACB","ACB"]7
# keys represent the candidates8
# index of array in dict represent the rank9
# value of array item represent number of votes casted10
# ref: https://www.programiz.com/python-programming/methods/built-in/sorted12
# T=O(mn + mlgm), S=O(mn)14
# m=number of candidates and m(number of ranks) is constant(26)15
def rankTeams(self, votes: List[str]) -> str:19
# n=number of votes, m=number of candidates(26)21
for i, c in enumerate(vote):25
d[c] = [0] * len(vote)26
# increment the count of votes for each rank29
# sort the dict keys in ascending order because if there is a tie we return in ascending order30
# sorted uses a stable sorting algorithm32
vote_names = sorted(d.keys()) # d.keys()=["A", "B", "C"]33
# sort the dict keys based on votes for each rank in descending order35
# sorted() always returns a list36
vote_rank = sorted(vote_names, reverse=True, key=lambda x: d[x])38
return "".join(vote_rank)