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.
Definition
- Class:
- HorseRacing
- Method:
- validateTicket
- Parameters:
- String[], String
- Returns:
- int
- Method signature:
- int validateTicket(String[] races, String ticket)
- (be sure your method is public)
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
{"AbX", "CdeF"}"AC"
Returns: 2
This is a valid ticket that correctly predicts the winners of both races.{"AbX", "CdeF"}"CA"
Returns: 2
This is a valid ticket that correctly predicts the winners of both races.{"AbX", "CdeF"}"Cb"
Returns: 1
{"AbX", "CdeF"}"X"
Returns: 0
{"AbX", "CdeF"}"HelloTopcoder"
Returns: -1
{"a", "b", "c", "d", "e", "f"}"bead"
Returns: 4