In a special ranking system, each voter gives a rank from highest to lowest to all teams participated in the competition.
The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second position to resolve the conflict, if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, we rank them alphabetically based on their team letter.
Given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.
Return a string of all teams sorted by the ranking system.
 
Example 1:
Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
Output: "ACB"
Explanation: Team A was ranked first place by 5 voters. No other team was voted as first place so team A is the first team.
Team B was ranked second by 2 voters and was ranked third by 3 voters.
Team C was ranked second by 3 voters and was ranked third by 2 voters.
As most of the voters ranked C second, team C is the second team and team B is the third.

Example 2:
Input: votes = ["WXYZ","XYZW"]
Output: "XWYZ"
Explanation: X is the winner due to tie-breaking rule. X has same votes as W for the first position but X has one vote as second position while W doesn't have any votes as second position. 

Example 3:
Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
Explanation: Only one voter so his votes are used for the ranking.

Example 4:
Input: votes = ["BCA","CAB","CBA","ABC","ACB","BAC"]
Output: "ABC"
Explanation: 
Team A was ranked first by 2 voters, second by 2 voters and third by 2 voters.
Team B was ranked first by 2 voters, second by 2 voters and third by 2 voters.
Team C was ranked first by 2 voters, second by 2 voters and third by 2 voters.
There is a tie and we rank teams ascending by their IDs.

Example 5:
Input: votes = ["M","M","M","M"]
Output: "M"
Explanation: Only team M in the competition so it has the first rank.

 
Constraints:

1 <= votes.length <= 1000
1 <= votes[i].length <= 26
votes[i].length == votes[j].length for 0 <= i, j < votes.length.
votes[i][j] is an English upper-case letter.
All characters of votes[i] are unique.
All the characters that occur in votes[0] also occur in votes[j] where 1 <= j < votes.length.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Sorting

Data structures

Mathematics

Starter code: class Solution:
    def rankTeams(self, votes: List[str]) -> str:
        

Solutions:
class Solution:

	def rankTeams(self, votes: List[str]) -> str:
		mem = {}
		for vote in votes:
			for i in range(len(vote)):
				team = vote[i]
				if team not in mem:
					mem[team] = [0 for _ in range(len(vote))]
				mem[team][i] += 1
		standings = []
		for (k, v) in mem.items():
			standings.append(tuple(v) + (-ord(k), k))
		standings.sort(reverse=True)
		res = [s[-1] for s in standings]
		return ''.join(res)


class Solution:

	def rankTeams(self, votes: List[str]) -> str:
		count = {v: [0] * len(votes[0]) for v in votes[0]}
		for vote in votes:
			for (i, v) in enumerate(vote):
				count[v][i] -= 1
		sort = sorted(list(count.items()), key=lambda x: (x[1], x[0]))
		ans = ''
		for (c, _) in sort:
			ans += c
		return ans


from heapq import heappush, heappop
from collections import defaultdict

class Solution(object):

	def rankTeams(self, vs):
		sz = len(vs[0])
		cnt = defaultdict(lambda : [0] * (sz + 1))
		for v in vs:
			for (i, c) in enumerate(v):
				cnt[c][i] -= 1
				cnt[c][sz] = c
		az = list(cnt.keys())
		az.sort(key=lambda cc: cnt[cc])
		return ''.join(az)


class Solution:

	def rankTeams(self, votes: List[str]) -> str:
		N = len(votes[0])
		mapping = collections.defaultdict(lambda : [0 for _ in range(N)])
		for v in votes:
			for (i, char) in enumerate(v):
				mapping[char][i] += 1
		return ''.join([candidate for (candidate, _) in sorted(mapping.items(), key=lambda x: (x[1], -ord(x[0])), reverse=True)])


class Solution:

	def rankTeams(self, votes: List[str]) -> str:
		m = len(votes[0])
		n = len(votes)
		dic = {}
		for vote in votes:
			for i in range(m):
				if vote[i] not in dic:
					dic[vote[i]] = [0] * m
				dic[vote[i]][i] += 1
		li = []
		for (k, v) in list(dic.items()):
			values = [str(n - i) for i in v]
			values.append(k)
			li.append(''.join(values))
		li.sort()
		return ''.join([item[-1] for item in li])


class Solution:

	def rankTeams(self, votes: List[str]) -> str:
		teams = list(votes[0])
		ranks = {}
		for t in teams:
			ranks[t] = [0] * len(teams)
			for v in votes:
				ranks[t][v.index(t)] -= 1
		return ''.join((t for (_, t) in sorted(((v, t) for (t, v) in list(ranks.items())))))