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
You are also given the
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
{"AbX", "CdeF"}
"CC"
Returns: -1
No repeats.
{"AbX", "CdeF"}
"z"
Returns: -1
No horses outside the races.
{"AbX", "CdeF"}
"ed"
Returns: -1
No two horses from the same race.
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}
"A"
Returns: 1
{"abc", "d" }
"ab"
Returns: -1
{"AB", "CD" }
"X"
Returns: -1
{"abc" }
"ab"
Returns: -1
{"AB", "CD" }
"AB"
Returns: -1
{"AB", "CD" }
"CD"
Returns: -1
{"ABC" }
"AB"
Returns: -1
{"AB", "CD" }
"AA"
Returns: -1
{"A", "B" }
"ABC"
Returns: -1
{"AbX", "CdeF" }
"bb"
Returns: -1
{"ABC", "DE" }
"AB"
Returns: -1
{"wleitE", "BrazIL", "gh" }
"wBw"
Returns: -1
{"AbX", "CdeF" }
"bX"
Returns: -1
{"AbX", "PQ" }
"Ab"
Returns: -1
{"A" }
"AA"
Returns: -1
{"ABC", "DEF" }
"AB"
Returns: -1
{"A" }
"hello"
Returns: -1
{"abc", "def" }
"abc"
Returns: -1
{"a", "b", "c", "d", "e", "f" }
"beaad"
Returns: -1
{"bc", "ad" }
"ad"
Returns: -1
{"ab", "cd" }
"ab"
Returns: -1
{"abc", "def", "ghi" }
"ab"
Returns: -1
{"AbX", "CdeF" }
"HelloTopcoder"
Returns: -1
{"ab", "cd" }
"x"
Returns: -1
{"A", "B" }
"AA"
Returns: -1
{"AbX", "CdeF" }
"Ab"
Returns: -1