# Solution For Rank Teams By Votes

Problem Statement

In this problem, we are given a list of team votes in which each element is a string containing the names of team members who voted for that team. Our task is to rank the teams based on the number of votes they received.

Rules for ranking:

• Teams with higher number of votes come first.
• If two or more teams have the same number of votes, then they are ranked alphabetically based on the names of team members who voted for them.

For example, if there are three teams, A, B and C, and A and B have the same number of votes, then the team with the members whose names come first alphabetically is ranked higher.

Solution

To solve this problem, we need to perform the following steps:

1. Create a dictionary with the key as the team name and the value as a list containing the number of votes and the list of names of team members who voted for that team.
2. Iterate over the given votes list and update the dictionary accordingly.
3. Sort the dictionary based on the number of votes and, in case of the same number of votes, sort based on the names of team members who voted for them.
4. Return the sorted list of team names.

Here is the Python code that implements the above approach:

“`
class Solution:
def rankTeams(self, votes: List[str]) -> str:
for i, team in enumerate(vote):

``````    sorted_teams = sorted(team_votes.keys(), key=lambda x: (-team_votes[x][0], x))

return "".join(sorted_teams)
``````

“`

In the above code:

• We first create an empty dictionary `team_votes`.
• We then iterate over the given `votes` list. For each vote, we iterate over the characters in the vote and update the dictionary `team_votes` accordingly. We keep a list of zeros with length equal to the length of each vote, which helps to keep track of the number of votes received by each team at each position.
• After updating the dictionary, we sort it based on the number of votes received by each team. For this, we use the `sorted()` method with the `key` argument as a lambda function. The lambda function returns a tuple of the negative votes received by each team at the first position and the name of the team. We take the negative of the votes to sort it in descending order. In case of a tie, we use the team name to break the tie.
• Finally, we return the sorted list of team names as a string.

Time Complexity

The time complexity of this solution is O(NM log M), where N is the number of teams and M is the length of each vote string.

• In the worst case, we need to iterate over all the votes, which takes O(NM) time.
• Sorting each team’s votes and sorting all the teams based on votes and names takes O(M log M) time for each team, which sums to O(NM log M).

Space Complexity

The space complexity of this solution is O(NM), which is used to store the `team_votes` dictionary.

## Step by Step Implementation For Rank Teams By Votes

```import java.util.*;

class Solution {

// create a map to store the vote counts for each team

Map map = new HashMap<>();

for (String vote : votes) {

for (int i = 0; i < vote.length(); i++) {

char c = vote.charAt(i);

if (!map.containsKey(c)) {

map.put(c, new int[26]);

}

map.get(c)[i]++;

}

}

// create a list of teams, sorted by their vote counts

List list = new ArrayList<>(map.keySet());

Collections.sort(list, (a, b) -> {

for (int i = 0; i < 26; i++) {

if (map.get(a)[i] != map.get(b)[i]) {

return map.get(b)[i] - map.get(a)[i];

}

}

return a - b;

});

// convert the list of teams back to an array

String[] res = new String[list.size()];

for (int i = 0; i < list.size(); i++) {

res[i] = list.get(i).toString();

}

return res;

}

}```
```def rankTeams(self, votes: List[str]) -> str:
d = {}
for i,v in enumerate(vote):
if v not in d:
d[v] = [0]*len(vote)
d[v][i] -= 1
return "".join(sorted(d.keys(), key=lambda x: (d[x],x)))```
```var rankTeams = function(votes) {

// create a hashmap to store the vote counts for each team
// key: team, value: vote count
let voteCount = {};
for (let i = 0; i < votes.length; i++) {
for (let j = 0; j < votes[i].length; j++) {
voteCount[votes[i][j]] = [0, 0, 0, 0, 0];
}
}
}

// create a list of teams sorted by their vote count
let teams = Object.keys(voteCount);
teams.sort((a, b) => {
for (let i = 0; i < 5; i++) {
if (voteCount[a][i] !== voteCount[b][i]) {
return voteCount[b][i] - voteCount[a][i];
}
}
return a.charCodeAt(0) - b.charCodeAt(0);
});

return teams;
};```
```vector rankTeams(vector& votes) {
map> m;
for(int i = 0; i < n; i++) {
m[vote[i]][i]++;
}
}
vector>> v;
for(auto itr : m) {
v.push_back({itr.first, {}});
for(auto it : itr.second) {
v.back().second.push_back(it.second);
}
}
sort(v.begin(), v.end(), [&](pair>& a, pair>& b){
for(int i = 0; i < n; i++) {
if(a.second[i] == b.second[i]) continue;
return a.second[i] > b.second[i];
}
return a.first < b.first;
});
vector ans;
for(auto it : v) {
ans.push_back(string(1, it.first));
}
return ans;
}```
```Assuming we have the following input: