Problem statement

https://binarysearch.com/problems/Team-Voting/

Solution

For each candidate keep 27 places: first 26 is how many times this candidate were choosen with number i and the last one its name.

Complexity

It is O(m*n + m^2 log m), where m is the size of alphabet.

Code

class Solution:
    def solve(self, votes):
        d = defaultdict(lambda: [0]*27)
        for V in votes:
            for i, x in enumerate(V):
                d[x][i] -= 1

        for x in d:
            d[x][-1] = x

        return "".join(x[-1] for x in sorted(d.values()))