Rank Teams by Votes

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, consider the second position to resolve the conflict. If they tie again, continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, 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 system. Sort all teams according to the ranking system described above.

Return a string of all teams sorted by the ranking system.

Example 1
Inputvotes = ["ABC","ACB","ABC","ACB","ACB"]
Output"ACB"
Team A is ranked first by all voters, and team C ranks ahead of team B because it has more second-place votes.
Example 2
Inputvotes = ["WXYZ","XYZW"]
Output"XWYZ"
X wins the tie with W because both have the same first-position votes, but X has one second-position vote while W has none.

Constraints

  • 1 <= votes.length <= 1000
  • 1 <= votes[i].length <= 26
  • votes[i].length == votes[j].length for 0 <= i, j < 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 <= j < votes.length.

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate