All problems
0531MediumArrayHash TableStringSortingCounting

Ranking Dessert Entries by Judge Cards

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1366Rank Teams by Votes

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def rank_entries(ballots: list[str]) -> str:
Java
public String rankEntries(String[] ballots)
September 7
Apply