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.Problem Overview: Each voter submits a ranking of teams. Higher positions carry more weight than lower ones. The task is to aggregate all votes and return the final team order based on how many first-place, second-place, third-place votes, and so on each team receives.
Approach 1: Counting Votes by Position (O(v * n + n log n * n) time, O(n^2) space)
The key observation: rankings are decided lexicographically by vote counts at each position. For example, if two teams have the same number of first-place votes, compare second-place votes, then third-place votes. Create a count[team][position] structure that records how many times each team appears at each ranking position across all votes. Iterate through every vote string and increment the appropriate counter.
Once the counting phase is complete, sort the list of teams. The sorting comparator checks vote counts from position 0 to n-1. The team with more votes at the earliest differing position ranks higher. If two teams have identical counts at every position, break the tie using the team letter (lexicographical order). This approach works well because the number of teams is small (at most 26), making the comparison cost manageable during sorting.
The algorithm relies heavily on array indexing for vote storage and counting frequency occurrences per position. Sorting then determines the final ranking.
Approach 2: Using a Custom Comparator (O(v * n + n log n * n) time, O(n^2) space)
This method builds the same vote frequency matrix but focuses on designing a comparator that directly compares two teams during sorting. Instead of preprocessing rankings into a separate structure, the comparator checks their vote distribution arrays position by position whenever the sorting algorithm compares them.
The comparator logic: iterate through all ranking positions and return the team with the higher count at the first mismatch. If all counts match, return the lexicographically smaller team ID. Languages like Java, Python, and C++ make this clean using custom comparison functions or key transformations.
This approach emphasizes sorting behavior and comparison logic. It mirrors how ranking systems work in real competitions where multiple tie-breaking layers exist.
Recommended for interviews: The counting-by-position approach is what interviewers usually expect. It shows that you recognized the lexicographic ranking rule and converted it into a structured frequency matrix before sorting. Implementing the custom comparator on top of that matrix demonstrates strong understanding of string iteration, counting, and multi-key sorting. Brute force reasoning about vote comparisons shows intuition, but the counting strategy demonstrates algorithmic clarity.
This approach involves counting the votes each team receives for each position. We store these counts in a dictionary, where each team has a list representing its vote count for each position. Then, by sorting the teams based on these counts (and alphabetically in case of ties), we can determine the final ranking.
We initialize a 2D array to keep track of how many votes each team receives for each ranking position. We iterate through each vote to populate this array. Then, we sort the teams using a custom comparator that first compares based on vote counts, breaking ties alphabetically if necessary.
The time complexity is O(N * P * log(P)), where N is the number of votes and P is the number of positions (or teams). The space complexity is O(P^2) due to the vote count storage.
This approach defines a custom comparator function to sort the teams according to the given ranking rules. We count the votes similarly, but rather than sorting explicitly with nested loops, we rely on a sophisticated comparator that resolves conflicts directly during the sorting process.
This solution sets up a custom struct and comparator for sorting teams based on the detailed voting rules. We use qsort with a comparator function to handle the sorting of voteRank objects of teams.
O(P * log(P)) is the time complexity, and O(P) for space due to temporary storage and object sorting.
For each candidate, we can count the number of votes they receive at each rank, then compare the vote counts for different ranks in order. If the vote counts are the same, we compare the letters.
The time complexity is O(n times m + m^2 times log m), and the space complexity is O(m^2). Here, n is the length of votes, and m is the number of candidates, i.e., the length of votes[0].
| Approach | Complexity |
|---|---|
| Counting Votes by Position | The time complexity is O(N * P * log(P)), where N is the number of votes and P is the number of positions (or teams). The space complexity is O(P^2) due to the vote count storage. |
| Using a Custom Comparator | O(P * log(P)) is the time complexity, and O(P) for space due to temporary storage and object sorting. |
| Counting + Custom Sorting | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Counting Votes by Position | O(v * n + n log n * n) | O(n^2) | Standard solution. Best when team count is small and rankings require multi-level tie-breaking. |
| Custom Comparator with Vote Matrix | O(v * n + n log n * n) | O(n^2) | Useful when the language provides flexible comparator functions for sorting complex ranking rules. |
1366. Rank Teams by Votes (LeetCode) • hakunamatasq • 6,588 views views
Watch 9 more video solutions →Practice Rank Teams by Votes with our built-in code editor and test cases.
Practice on FleetCode