Problem Statement
Given a world map, you must determine the country that has the largest territory. The map is given as a String[] worldMap, where each character represents a square with an area of 1. Unoccupied squares are represented by space characters (' '). Occupied squares are represented by uppercase letters ('A'-'Z') that denote the occupying country. The area of a country's territory is the total area of its occupied squares. Return a String containing exactly one character - the name of the largest country. In case of a tie, return the largest country whose name comes first alphabetically.
Definition
- Class:
- LargestCountry
- Method:
- getLargest
- Parameters:
- String[]
- Returns:
- String
- Method signature:
- String getLargest(String[] worldMap)
- (be sure your method is public)
Constraints
- worldMap will contain between 1 and 50 elements, inclusive.
- Each element of worldMap will contain between 1 and 50 characters, inclusive.
- All elements of worldMap will contain the same number of characters.
- Each element of worldMap will contain only uppercase letters ('A'-'Z') and spaces (' ').
- At least one element of worldMap will contain a non-space character.
Examples
{"AAA","CA "}Returns: "A"
Country "A" has the largest territory.{"ACA","CAC"}Returns: "A"
Countries "A" and "C" both occupy 3 squares. "A" is returned because it comes earlier alphabetically.{"ABZ","CXC"}Returns: "C"
"C" is the largest country.{"AAA","CA "}Returns: "A"
{" A "," B "," X "," B "," X "," X "}Returns: "X"