Connection Status:
Competition Arena > RabbitVoting
SRM 475 · 2009-11-12 · by lyrically · Simple Search, Iteration
Class Name: RabbitVoting
Return Type: String
Method Name: getWinner
Arg Types: (vector<string>, vector<string>)
Problem Statement

Problem Statement

Rabbits often feel lonely, so one group of rabbits decided to get together and hold a beauty contest to determine who among them has the most beautiful ears. The rules are as follows. Each rabbit submits one vote. If a rabbit votes for himself/herself, that vote is considered invalid and thrown away. In the end, the rabbit who receives the most valid votes is the winner.

You are given a String[] names and a String[] votes. The i-th rabbit is named names[i], and he/she voted for the rabbit named votes[i]. All rabbits have distinct names. Return the name of the rabbit who received the most valid votes. If there is a tie for most votes, return an empty String instead.

Notes

  • Rabbit names are case-sensitive. See example 4 for clarification.

Constraints

  • names will contain between 2 and 50 elements, inclusive.
  • Each element of names will contain between 1 and 50 characters, inclusive.
  • Each character in names will be a letter ('A'-'Z', 'a'-'z').
  • All elements of names will be distinct.
  • votes will contain the same number of elements as names.
  • Each element of votes will be the same as one of the elements of names.
Examples
0)
{ "Alice", "Bill", "Carol", "Dick" }
{ "Bill", "Dick", "Alice", "Alice" }
Returns: "Alice"

2 votes for Alice, 1 for Bill, 0 for Carol, and 1 for Dick. Alice got the most.

1)
{ "Alice", "Bill", "Carol", "Dick" }
{ "Carol", "Carol", "Bill", "Bill" }
Returns: ""

Bill and Carol are tied with 2 votes each.

2)
{ "Alice", "Bill", "Carol", "Dick" }
{ "Alice", "Alice", "Bill", "Bill" }
Returns: "Bill"

Alice's vote for herself is invalid. 1 valid vote for Alice, 2 for Bill.

3)
{ "Alice", "Bill" }
{ "Alice", "Bill" }
Returns: ""

All votes are invalid.

4)
{ "WhiteRabbit", "whiterabbit", "whiteRabbit", "Whiterabbit" }
{ "whiteRabbit", "whiteRabbit", "whiteRabbit", "WhiteRabbit" }
Returns: "whiteRabbit"

These four are different names.

Submissions are judged against all 100 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class RabbitVoting with a public method string getWinner(vector<string> names, vector<string> votes) · 100 test cases · 2 s / 256 MB per case

Submitting as anonymous