Ctrl K

Ranking Finalists by Jury Ballots

Medium
The finalists of a contest are labelled with distinct uppercase English letters. Each judge hands in a ballot: a string listing every finalist from best to worst. The ballots are collected in the array ballots. The final standings work like this: whoever was placed first on more ballots ranks higher. If that is a tie, compare how many second places they got, then third places, and so on. If two finalists have equal counts for every place, the one whose letter comes earlier in the alphabet ranks higher. Return a string with the letters of all finalists in the order of the final standings.
Example 1
ballots=["PQR", "QPR", "PRQ"] → 'PQR'

P is first on two ballots, Q on one and R on none, so first places alone already give the order P, Q, R.

Example 2
ballots=["MN", "NM"] → 'MN'

M and N tie on every place, so the alphabet decides: M comes first.

Example 3
ballots=["KDA"] → 'KDA'
Constraints
  • 1 ≤ len(ballots) ≤ 10⁵
  • 1 ≤ len(ballots[i]) ≤ 26
  • All ballots have the same length
  • A ballot consists of uppercase English letters with no repeats
  • Every ballot contains the same set of letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Searched Words MediumPalindromes From Shared Letters MediumAnagram Buckets MediumMore Than a Third of the Votes MediumMost Common Values Medium
loading Python…
Input
ballots=["PQR", "QPR", "PRQ"]
Expected
'PQR'

Run checks the first 3 tests in your browser. Submit checks all 61 tests on the server, hidden ones included.