Problem Statement
Class name: PhoneDialer
Method name: findFriend
Parameters: String[]
Returns: String
Lazy Joe picks up a phone with the intention of calling a friend. However, as
he is extremely lazy and has the habit of dialing with only a single finger, he
wishes to call the friend whose phone number involves moving his finger the
least distance. Your mission is to help Joe figure out who to call so that his
finger travels the shortest distance.
Implement a class PhoneDialer, which contains a method findFriend. The method
takes a String[], where its elements are formatted as "(###) ###-####, NAME"
where # represents a digit from 0 through 9, and NAME is the name of the person
associated with that phone number. The method then returns the name of the
person whose phone number requires the least amount of movement to dial (least
total distance). In the case that several people have phone numbers with the
same total distance, the method returns the name of the first one with that
minimum distance encountered in the String[].
The "distance" between any two keys on Joe's telephone is defined as the
"taxicab" distance between those keys on a telephone keypad with the
arrangement:
_1_ _2_ _3_
_4_ _5_ _6_
_7_ _8_ _9_
___ _0_ ___
In other words, the distance between two digits is the minimum number of steps
needed to get from one digit to the other, where each step involves moving from
one key to another in either a horizontal or a vertical (but not diagonal)
direction. For instance, '1' is a distance of 2 away from '5' because two
steps are needed to get from '1' to '5': either '1'-->'2'-->'5' or
'1'-->'4'-->'5'. There is no "wrapping around" ('7'-'9' is 2, not 1).
The total distance for any particular phone number is the sum of the distances
between each pair of consecutive digits. For example, the total distance of
the phone number (123) 456-7890 is equal to the sum of the distances from
'1'-->'2', '2'-->'3', '3'-->'4', '4'-->'5', '5'-->'6', '6'-->'7', '7'-->'8',
'8'-->'9', and '9'-->'0'. In this case, the total distance would be 1 + 1 + 3
+ 1 + 1 + 3 + 1 + 1 + 2 = 14. Note that getting to the initial button does not
incur any distance costs; thus a phone number such as "(555) 555-5555" has a
total distance of zero.
The method signature is:
public String findFriend (String[] directory);
*directory must contain at least one and no more than 45 entries.
*The format for each String in the directory String[] must follow "(###)
###-####, NAME" exactly. The quotation marks are shown to set off the string
only and are not included in the directory entry. Note that there are exactly
two mandatory spaces (" ") in the string: one after the area code, and one
before the name. NAME must be between 1 and 20 characters in length,
inclusive, and must consist of only letters or spaces.
*TopCoder will check the validity of the input.
Examples:
-If directory={"(242) 424-2424, Lord Byron","(525) 252-5252, Shakespeare"}, the
total distance for Lord Byron's number is 2+2+2+2+2+2+2+2+2=18 while the
distance for Shakespeare's number is 1+1+1+1+1+1+1+1+1=9. The method returns
"Shakespeare".
-If directory={"(555) 555-5557, Mark","(493) 294-4891, Jim","(888) 444-4444,
Mike"}, then the total distance for Jim's number is 3+2+1+3+3+0+2+1+4=19 while
the distance for Mark and Mike's numbers are both 2. Since Mark's number comes
before Mike's in the directory, the method returns "Mark".
-If directory={"(481) 572-3767, Pecos Bill","(582) 767-8273, Tarzan","(567)
286-4868, George of the Jungle","(910) 482-3572, Mother Goose"}, then the
method returns "George of the Jungle".
-If directory={"(111) 111-1111, Ed"}, the method returns " Ed". Note there
are 2 spaces between the comma and Ed, so the method return String has a
leading space.
Definition
- Class:
- PhoneDialer
- Method:
- findFriend
- Parameters:
- String[]
- Returns:
- String
- Method signature:
- String findFriend(String[] param0)
- (be sure your method is public)
Constraints
Examples
No public sample cases were explicitly marked for this problem in the original data.