Statistics

Problem Statement for "HorseRacing"

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

  1. {"AbX", "CdeF"}

    "AC"

    Returns: 2

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

  2. {"AbX", "CdeF"}

    "CA"

    Returns: 2

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

  3. {"AbX", "CdeF"}

    "Cb"

    Returns: 1

  4. {"AbX", "CdeF"}

    "X"

    Returns: 0

  5. {"AbX", "CdeF"}

    "HelloTopcoder"

    Returns: -1

  6. {"a", "b", "c", "d", "e", "f"}

    "bead"

    Returns: 4

  7. {"AbX", "CdeF"}

    "CC"

    Returns: -1

    No repeats.

  8. {"AbX", "CdeF"}

    "z"

    Returns: -1

    No horses outside the races.

  9. {"AbX", "CdeF"}

    "ed"

    Returns: -1

    No two horses from the same race.

  10. {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}

    "A"

    Returns: 1

  11. {"abc", "d" }

    "ab"

    Returns: -1

  12. {"AB", "CD" }

    "X"

    Returns: -1

  13. {"abc" }

    "ab"

    Returns: -1

  14. {"AB", "CD" }

    "AB"

    Returns: -1

  15. {"AB", "CD" }

    "CD"

    Returns: -1

  16. {"ABC" }

    "AB"

    Returns: -1

  17. {"AB", "CD" }

    "AA"

    Returns: -1

  18. {"A", "B" }

    "ABC"

    Returns: -1

  19. {"AbX", "CdeF" }

    "bb"

    Returns: -1

  20. {"ABC", "DE" }

    "AB"

    Returns: -1

  21. {"wleitE", "BrazIL", "gh" }

    "wBw"

    Returns: -1

  22. {"AbX", "CdeF" }

    "bX"

    Returns: -1

  23. {"AbX", "PQ" }

    "Ab"

    Returns: -1

  24. {"A" }

    "AA"

    Returns: -1

  25. {"ABC", "DEF" }

    "AB"

    Returns: -1

  26. {"A" }

    "hello"

    Returns: -1

  27. {"abc", "def" }

    "abc"

    Returns: -1

  28. {"a", "b", "c", "d", "e", "f" }

    "beaad"

    Returns: -1

  29. {"bc", "ad" }

    "ad"

    Returns: -1

  30. {"ab", "cd" }

    "ab"

    Returns: -1

  31. {"abc", "def", "ghi" }

    "ab"

    Returns: -1

  32. {"AbX", "CdeF" }

    "HelloTopcoder"

    Returns: -1

  33. {"ab", "cd" }

    "x"

    Returns: -1

  34. {"A", "B" }

    "AA"

    Returns: -1

  35. {"AbX", "CdeF" }

    "Ab"

    Returns: -1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: