Given an array of strings votes where each element is the name of the candidate a voter voted for, return the name of the candidate who received the most votes.
If there is a tie in the maximum number of votes, return the lexicographically smaller name among the tied candidates.
Function Signature
def find_winner(votes: list[str]) -> str:
Rules
-
Only the candidates tied at the maximum vote count are compared by name. A candidate with fewer votes never wins, however early it sorts.
-
Lexicographic order is ordinary string comparison: compare letter by letter, and a name that is a prefix of another name is smaller (
"al"
comes before
"alice"
).
Constraints
-
1 <= len(votes) <= 10^4
-
1 <= len(votes[i]) <= 10
-
votes[i]
consists of lowercase English letters.
Examples
Input: votes = ["alice","bob","alice","carol","bob","alice"]
Output: "alice"
alice has 3 votes, bob has 2 and carol has 1, so alice wins outright.
Input: votes = ["dave","eve","dave","eve"]
Output: "dave"
dave and eve are tied with 2 votes each, and "dave" is lexicographically smaller than "eve".
Input: votes = ["bob","carl","carl","bob","anna"]
Output: "bob"
bob and carl both have 2 votes and anna has 1. The tie is only between bob and carl, and "bob" comes before "carl". anna sorts first but has fewer votes.