Connection Status:
Competition Arena > HorseRacing
TCO20 South Asia Parallel · 2020-08-15 · by erinn · Brute Force
Class Name: HorseRacing
Return Type: int
Method Name: validateTicket
Arg Types: (vector<string>, string)
Problem Statement

Problem Statement

A horse racing event typically consists of several races, each of which contains several horses. Fans bet on one or more races, attempting to predict who will win each one. Each race has at least one horse, and no horse will run more than one race on a given day.

In this problem, each horse is represented by a character: 'A'-'Z' or 'a'-'z'. Case matters, e.g., 'X' and 'x' are two different horses.

You are given String[] races, each element representing the result of one of the races. For example, if races[0] = "AbX", race 0 was won by horse 'A', horse 'b' was second and horse 'X' was last in this race.

You are also given the String ticket, representing the ticket with some horses. Note that the horses on the ticket may not be in the same order as the races they appear in.

A valid ticket is one such that each selected horse is actually racing that day, no horse is repeated, and no more than one selection is made for any given race.

Determine whether the ticket is valid. If it is, return the number of correctly predicted race winners. If the ticket is invalid for any reason, return -1.

Constraints

  • races will be non-empty.
  • Each element of races will only contain letters ('A'-'Z', 'a'-'z').
  • Each element of races will be non-empty.
  • No letter will be repeated in races.
  • ticket will only contain letters ('A'-'Z', 'a'-'z').
  • ticket will contain between 1 and 50 characters, inclusive.
Examples
0)
{"AbX", "CdeF"}
"AC"
Returns: 2

This is a valid ticket that correctly predicts the winners of both races.

1)
{"AbX", "CdeF"}
"CA"
Returns: 2

This is a valid ticket that correctly predicts the winners of both races.

2)
{"AbX", "CdeF"}
"Cb"
Returns: 1
3)
{"AbX", "CdeF"}
"X"
Returns: 0
4)
{"AbX", "CdeF"}
"HelloTopcoder"
Returns: -1
6)
{"AbX", "CdeF"}
"CC"
Returns: -1

No repeats.

7)
{"AbX", "CdeF"}
"z"
Returns: -1

No horses outside the races.

8)
{"AbX", "CdeF"}
"ed"
Returns: -1

No two horses from the same race.

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

Coding Area

Language: C++17 · define a public class HorseRacing with a public method int validateTicket(vector<string> races, string ticket) · 35 test cases · 2 s / 256 MB per case

Submitting as anonymous