Rank Teams by Votes
Real-world version: Ranking Dessert Entries by Judge CardsThe statement here is our own: the same inputs, outputs and technique.
Each judge at a bake-off hands in a card ranking every entry from best to worst. An entry is written as one uppercase letter, and ballots[i] is the i-th judge's card, read left to right from their first place to their last. Every card lists exactly the same entries, each of them once.
The final table is worked out place by place. The entry named first on more cards finishes ahead. If two entries were named first on the same number of cards, whichever of them was named second on more cards finishes ahead, then third place is compared, and so on. If two entries have exactly the same count at every place, the one whose letter comes earlier in the alphabet finishes ahead.
Return the entries in final order as a single string.
Examples
Example 1
- Input
- ballots = ["ACB", "BCA", "CBA"]
- Output
- "CBA"
Each entry is named first on exactly one card, so first place is compared next: C is named second twice, B once and A never. The table therefore reads C, then B, then A.
Example 2
- Input
- ballots = ["ABC", "CBA", "CBA"]
- Output
- "CAB"
C is named first twice, A once and B never, which fixes the whole table as C, then A, then B.
Example 3
- Input
- ballots = ["BA", "AB"]
- Output
- "AB"
A and B are each named first once and second once, so their counts match at every place and the alphabet puts A ahead.
Constraints
- 1 <= ballots.length <= 1000
- 1 <= ballots[i].length <= 26
- All cards have the same length.
- ballots[i][j] is an uppercase English letter.
- No card names the same entry twice.
- Every card names exactly the entries that ballots[0] names.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def rank_entries(ballots: list[str]) -> str:public String rankEntries(String[] ballots)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's