Problem Statement
We are interested in applying this technique to other sports. Create a class MakeTeam that contains the method lastOne that takes n, the number of players needed to form a team, and the names of players in the order in which they came to the office as inputs and returns the name of the last player. If fewer than n players come to the office, return "NO TEAM".
Definition
- Class:
- MakeTeam
- Method:
- lastOne
- Parameters:
- int, String[]
- Returns:
- String
- Method signature:
- String lastOne(int n, String[] names)
- (be sure your method is public)
Notes
- The player removed from the first n may be the last player added (Example 2).
Constraints
- n is between 1 and 20, inclusive.
- names contains between 1 and 50 elements, inclusive.
- Each element of names contains between 1 and 20 characters, inclusive.
- Each element of names contains only uppercase letters 'A'-'Z'.
- The elements of names are distinct.
Examples
4
{"BO","CHUCK","AL","DON","TOM"}
Returns: "TOM"
BO, CHUCK, AL, and DON are the first 4. AL is removed (he came later than BO). That leaves TOM and AL, and TOM has the longer name.
2
{"BOB"}
Returns: "NO TEAM"
BOB is only one guy. We need 2 to make a team.
3
{"JIM","BOB","TOM","AL","HAL"}
Returns: "TOM"
JIM, BOB, and TOM are the first 3. TOM is removed, since earlier players are kept in the case of equal length names. Then TOM is added back to the team because his name is longer than AL and he came before HAL.
3
{"BILL","JOHN","TOMMY","HAL"}
Returns: "JOHN"
1
{"SEELENFREUND"}
Returns: "SEELENFREUND"
3
{"JACK","TERRY","HAL","JO","AL"}
Returns: "HAL"
6
{"SAM","SAP","SAX","BO","SAD","SSM","SAL","HAL","RED"}
Returns: "SAL"
2
{"STOTTLEMEYER","EMPENTHALLERSTEIN","AL","DEFORESTATION","JON"}
Returns: "DEFORESTATION"
20
{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T"}
Returns: "T"
19
{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T"}
Returns: "S"
3
{"B","BB","BBB","BBBB","BBBBB","BBBBBB","BBBBBBB","BBBBBBBB"}
Returns: "BBBBBBBB"
3
{ "AL", "BOB", "TOM" }
Returns: "AL"
1
{ "SAD" }
Returns: "SAD"
3
{ "A", "BBB", "CCC" }
Returns: "A"
1
{ "SAM" }
Returns: "SAM"
2
{ "ABCDE", "BCDEF", "ABCD", "DEFG" }
Returns: "BCDEF"
3
{ "BOB", "JOE", "CHRIS", "TOM" }
Returns: "JOE"
5
{ "AAA", "BBB", "CCC", "DDD", "EEE", "FFF" }
Returns: "EEE"
3
{ "BOOOOOO", "B", "A", "S", "Z" }
Returns: "A"
3
{ "AL", "ASD", "ADG" }
Returns: "AL"
4
{ "JIM", "AL", "HAL", "TOM" }
Returns: "AL"
3
{ "AL", "ADG", "AGS" }
Returns: "AL"
5
{ "AA" }
Returns: "NO TEAM"
5
{ "BOB" }
Returns: "NO TEAM"
11
{ "AA", "CC", "CD", "EEW" }
Returns: "NO TEAM"
6
{ "SAM", "SAP", "SAX", "BO", "SAD", "SSM", "SAL", "HAL", "RED" }
Returns: "SAL"
3
{ "A", "AA", "AAAA" }
Returns: "A"
6
{ "AAA", "BBB" }
Returns: "NO TEAM"
2
{ "AB", "ABC" }
Returns: "AB"
3
{ "JIM", "BOB", "TOM", "AL", "HAL" }
Returns: "TOM"
4
{ "JIM", "BOB", "AL", "HAL" }
Returns: "AL"
2
{ "AAAAA", "BBB", "FFFFFFFFF", "GGGGGGGGG" }
Returns: "FFFFFFFFF"
2
{ "STOTTLEMEYER", "EMPENTHALLERSTEIN", "AL", "DEFORESTATION", "JON" }
Returns: "DEFORESTATION"
2
{ "AL", "TIM", "FRED", "JOHN", "FRANK", "TELEPATHY", "YOU", "FORGOT" }
Returns: "TELEPATHY"
4
{ "FGH", "HGF", "ASD", "DSA", "QWE", "EWQ", "YUI", "UYT", "OPI", "POI" }
Returns: "DSA"