Problem Statement
PROBLEM STATEMENT
You work in a store, and you have a list of purchases that were made in the
last few hours. Your boss gives you a Coupon ID and asks you to determine,
from the list of purchases, what the coupon is for.
Each purchase is a space-separated String of items (alphabetic strings -
containing only the characters 'A' - 'Z' and 'a' - 'z') and coupons (numeric
strings - containing only the characters '0' - '9', inclusive). Each of these
item names and coupon IDs refers to a specific item or coupon in a
case-sensitive, string-wise manner. For example, coupon "00203" is different
than coupon "0203", and item "nails" is different than item "Nails".
Create a class Coupon that contains the method getItem, which takes a String[]
purchases and a String coupon and, based on purchases, returns the item for
which coupon must be for - or "unknown" (quotes for clarity only) if it cannot
be determined.
DEFINITION
Class: Coupon
Method: getItem
Parameters: String[], String
Returns: String
Method signature (be sure your method is public): String getItem(String[]
purchases, String coupon);
NOTES
- Items can be purchased without a coupon.
- A coupon can only be used for a single item.
- Coupons with the ID in question included in an element of purchases will NOT
be unused, though other coupons in the purchase might be. Multiple instances
of this coupon in a single purchase must be accompanied by at least the same
quantity of its corresponding item.
- Items and Coupons in each purchase can be separated by multiple spaces.
- Elements of purchase can have leading and/or trailing spaces.
- If there are multiple possible solutions for which item it is - return
"unknown" (quotes for clarity only).
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- coupon will be between 1 and 50 characters in length, inclusive.
- coupon will contain only digits ('0' - '9', inclusive).
- purchases will contain between 1 and 50 elements, inclusive.
- Each element of purchases will be between 1 and 50 characters in length,
inclusive.
- Each element of purchases will contain only the characters 'A' - 'Z', 'a' -
'z', '0' - '9' and space.
- Each space-separated string in each element of purchases will be either only
digits ('0' - '9', inclusive), or only letters ('A' - 'Z' and 'a' - 'z').
- There will be at least one item for which this coupon could be. In other
words, there will be at least one valid solution.
EXAMPLES
In the following examples, quotes, commas and braces are for clarity only.
They are never part of the input or output.
(1)
purchases = {"Tuna Rice Milk 3 5", "Milk Tuna 1 3", "Tuna Rice 3 4"}
coupon = "3"
return: "Tuna"
You know that coupon 3 must be for "Tuna" since that is the only item common to
all the purchases using that coupon.
(2)
purchases = {"Hammer Nail Nail Hammer 1 1 2 2","Hammer Nail Nail 1 2 2"}
coupon = "1"
return: "unknown"
This coupon could be for either "Hammer" or "Nail".
(3)
purchases = {" 1 Hammer 2 Nail 1 Hammer "," Hammer 1 Nail 2 Nail 2 "}
coupon = "1"
return: "Hammer"
This coupon cannot be for "Nail" since two are used in the first purchase, and
there is only one "Nail" purchased then.
(4)
purchases = {"Stuff"}
coupon = "1"
return: "unknown"
The coupon was not used, so it is impossible to say what it is for.Definition
- Class:
- Coupon
- Method:
- getItem
- Parameters:
- String[], String
- Returns:
- String
- Method signature:
- String getItem(String[] param0, String param1)
- (be sure your method is public)
Constraints
Examples
No public sample cases were explicitly marked for this problem in the original data.