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.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= votes.length &lt;= 1000
	1 &lt;= votes[i].length &lt;= 26
	votes[i].length == votes[j].length for 0 &lt;= i, j &lt; votes.length.
	votes[i][j] is an English uppercase letter.
	All characters of votes[i] are unique.
	All the characters that occur in votes[0] also occur in votes[j] where 1 &lt;= j &lt; votes.length.


#["ABC","ACB","ABC","ACB","ACB"]
#d = {
#    "A": [5, 0, 0],
#    "B": [0, 2, 3],
#    "C": [0, 3, 2]
#}
#keys represent the candidates
#index of array in dict represent the rank
#value of array item represent number of votes casted
#ref: https://www.programiz.com/python-programming/methods/built-in/sorted
class Solution:
    #T=O(mn + mlgm), S=O(mn)
	#n=number of votes
	#m=number of candidates and m(number of ranks) is constant(26)
    def rankTeams(self, votes: List[str]) -> str:
        d = {}
        #build the dict
        #T=O(mn), S=O(mn)
		#n=number of votes, m=number of candidates(26)
        for vote in votes:
            for i, c in enumerate(vote):
                #if key not in dict
                if c not in d:
                    #d[char] = [0, 0, 0]
                    d[c] = [0]*len(vote)
                #increment the count of votes for each rank
                #d["A"][0] = 1
                d[c][i] += 1
        #sort the dict keys in ascending order because if there is a tie we return in ascending order
		#sorted uses a stable sorting algorithm
        #T=O(mlgm), S=O(m)
        vote_names = sorted(d.keys()) #d.keys()=["A", "B", "C"]
        #sort the dict keys based on votes for each rank in descending order
        #T=O(mlgm), S=O(m)
        #sorted() always returns a list
        vote_rank = sorted(vote_names, reverse=True, key= lambda x: d[x])
        #join the list
        return "".join(vote_rank)
class Solution {
    public String rankTeams(String[] votes) {
        int n = votes.length;
        int teams = votes[0].length();
        Map<Character, int[]> map = new HashMap<>();
        List<Character> chars = new ArrayList<>();

        for(int i = 0 ; i < teams ; i++) {
            char team = votes[0].charAt(i);
            map.put(team, new int[teams]);
            chars.add(team);
        }

        for(int i = 0 ; i < n ; i++) {
            String round = votes[i];
            for(int j = 0 ; j < round.length() ; j++) {
                map.get(round.charAt(j))[j]+=1;
            }
        }

        chars.sort((a,b) -> {
            int[] l1 = map.get(a);
            int[] l2 = map.get(b);
            for(int i = 0 ; i < l1.length; i++) {
                if(l1[i] < l2[i]) {
                    return 1;
                }
                else if(l1[i] > l2[i]) {
                    return -1;
                }
            }
            return a.compareTo(b);
        });

        StringBuilder sb = new StringBuilder();
        for(char c : chars) {
            sb.append(c);
        }
        return sb.toString();
    }
}
class Solution {
public:

    static bool cmp(vector<int>a, vector<int>b){

        for(int i = 1; i<a.size(); i++){
            if(a[i]!=b[i]){
                return a[i]>b[i];
            }
        }

        return a[0]<b[0];
    }

    string rankTeams(vector<string>& votes) {

        int noofteams = votes[0].size();
        string ans = "";
        vector<vector<int>>vec(noofteams, vector<int>(noofteams+1, 0));

        unordered_map<char, int>mp;
        for(int i = 0; i<votes[0].size(); i++){
            mp[votes[0][i]] = i;
            vec[i][0] = votes[0][i]-'a';
        }

        for(string x: votes){
            for(int i = 0; i<x.size(); i++){
                vec[mp[x[i]]][i+1]++;
            }
        }

        sort(vec.begin(), vec.end(), cmp);

        for(int i = 0; i<vec.size(); i++){
            ans.push_back(vec[i][0]+'a');
        }

        return ans;
    }
};
var rankTeams = function(votes) {
     if(votes.length == 1)
        return votes[0];
    let map = new Map()
    for(let vote of votes){
        for(let i = 0; i < vote.length;i++){
            if(!(map.has(vote[i]))){
            //create all the values set as zero
                map.set(vote[i],Array(vote.length).fill(0))
            }
            let val = map.get(vote[i])
            val[i] = val[i] + 1
            map.set(vote[i], val)
        }
    }

    let obj = [...map.entries()]; //converting as array ["A",[5,0,0]]
    obj = obj.sort((a,b) => {
        for(let i = 0; i < a[1].length;i++){
            if(a[1][i] > b[1][i])
                return -1;
            else if(a[1][i] < b[1][i])
                return 1;
        }
        // if all chars are in same positon return the value charcode
        return a[0].charCodeAt(0) - b[0].charCodeAt(0);
    })
    return obj.map(item => item[0]).join('')
};
Rank Teams by Votes