Problem Statement
It's time to get something to eat and I've come across a sandwich bar. Like most people, I prefer certain types of sandwiches. In fact, I keep a list of the types of sandwiches I like.
The sandwich bar has certain ingredients available. I will list the types of sandwiches I like in order of preference and buy the first sandwich the bar can make for me. In order for the bar to make a sandwich for me, it must include all of the ingredients I desire.
Given a String[] available, a list of ingredients the sandwich bar can use, and a String[] orders, the types of sandwiches I like, in order of preference (most preferred first), return the 0-based index of the sandwich I will buy. Each element of orders represents one type of sandwich I like as a space-separated list of ingredients in the sandwich. If the bar can make no sandwiches I like, return -1.
Definition
- Class:
- SandwichBar
- Method:
- whichOrder
- Parameters:
- String[], String[]
- Returns:
- int
- Method signature:
- int whichOrder(String[] available, String[] orders)
- (be sure your method is public)
Constraints
- available will contain between 1 and 50 elements, inclusive.
- Each element of available will contain between 1 and 50 lowercase letters ('a' - 'z'), inclusive.
- orders will contain between 1 and 50 elements, inclusive.
- Each element of orders will contain between 1 and 50 lowercase letters ('a' - 'z') and spaces, inclusive.
- Each element of orders will represent a list of desired ingredients separated by single spaces, with no leading or trailing spaces.
Examples
{ "ham", "cheese", "mustard" }{ "ham cheese" }Returns: 0
I only like plain ham and cheese sandwiches, and since both ham and cheese are available, I'll take that.{ "cheese", "mustard", "lettuce" }{ "cheese ham", "cheese mustard lettuce", "ketchup", "beer" }Returns: 1
They've run out of ham, but I'll consider other options now.{ "cheese", "cheese", "cheese", "tomato" }{ "ham ham ham", "water", "pork", "bread", "cheese tomato cheese", "beef" }Returns: 4
Ignore any duplicate elements in the lists.{ "foo", "bar", "baz", "gazonk", "quux", "bat", "xyzzy", "shme", "hukarz", "grault", "waldo", "bleh" }{ "kalatehas", "spam eggs", "needle haystack", "bleh blarg", "plugh", "the best sandwich in the universe" }Returns: -1