#1366
Rank Teams by Votes
specialist · 725 · lc medium +31 · verified · 60% accepted · 1,563 likes · top 58%
Description
In a ranked voting system, each ballot in votes orders all teams from most to least preferred. Teams are ranked by first-place vote count; ties are broken using second-place counts, then third-place, and so on. If a tie persists after all positions, teams are sorted alphabetically by their letter. Return a single string listing all teams in final ranking order.
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.
Code
1
2
3