In a special ranking system, each voter gives a rank from highest to lowest to all teams participating 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.
You are 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 ranked third by 3 voters. Team C was ranked second by 3 voters and 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 the tie-breaking rule. X has the same votes as W for the first position, but X has one vote in the second position, while W does not have any votes in the second position.
Example 3:
Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"] Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK" Explanation: Only one voter, so their votes are used for the ranking.
Constraints:
1 <= votes.length <= 10001 <= votes[i].length <= 26votes[i].length == votes[j].length for 0 <= i, j < votes.length.votes[i][j] is an English uppercase letter.votes[i] are unique.votes[0] also occur in votes[j] where 1 <= j < votes.length.In #1366 Rank Teams by Votes, each vote represents a ranking of teams. The goal is to determine the final ranking by prioritizing teams with more votes in higher positions. If two teams tie at a position, the comparison continues to the next position until a difference is found.
A practical approach is to build a vote count matrix where each team stores how many votes it received for each rank. For example, if there are n teams, each team maintains an array of size n representing counts for first place, second place, and so on.
After collecting counts from all votes, sort the teams using a custom comparator. Teams are compared lexicographically based on their vote count arrays (higher counts first). If all ranks tie, use the team letter as a final tie-breaker.
This approach leverages arrays, counting, and custom sorting. The counting step processes all votes, while sorting determines the final ranking efficiently.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Counting votes + custom sorting | O(m * n + n log n * n) | O(n^2) |
CrioDo
Use these hints if you're stuck. Try solving on your own first.
Build array rank where rank[i][j] is the number of votes for team i to be the j-th rank.
Sort the teams by rank array. if rank array is the same for two or more teams, sort them by the ID in ascending order.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems like Rank Teams by Votes appear in interviews because they test sorting with custom comparators, counting techniques, and handling tie-breaking rules. Similar ranking and aggregation problems are common in FAANG-style coding interviews.
An array or hash map combined with a 2D counting structure works best. Each team maintains an array storing how many votes it received for each position. This structure allows efficient comparison during sorting.
The optimal approach counts how many votes each team receives at every ranking position and then sorts teams using a custom comparator. Teams with more higher-rank votes are prioritized. If all ranks tie, the team with the smaller lexicographical name wins.
Custom sorting is needed because ranking depends on multiple criteria. Teams must be compared position by position based on vote counts. If all positions are equal, the alphabetical order of the team name is used as the final tie-breaker.