Connection Status:
Competition Arena > PhonePad
SRM 244 · 2005-05-23 · by logged · Simple Search, Iteration, Simulation
Class Name: PhonePad
Return Type: int
Method Name: fingerMovement
Arg Types: (string)
Problem Statement

Problem Statement

A typical phone pad has the following layout:
 1 2 3
 4 5 6
 7 8 9
 * 0 #
The finger movement distance between any pair of keys is defined as their Manhattan distance (the sum of the absolute differences of their coordinates) on the phone pad. For example, the key '6' is 0 units away from itself, 1 unit away from '3', '5', and '9', 2 units away from '2', '4', '8', and '#', 3 units away from '1', '7', and '0', and 4 units away from '*'. Write a class PhonePad with a method fingerMovement that takes a String, phoneNumber, and returns the minimal distance of total finger movement distance required to dial the phone number. Note that the finger is initially on key '5'.

Constraints

  • phoneNumber will be between 3 and 20 characters long, inclusive.
  • phoneNumber will consist of only digits ('0'-'9').
Examples
0)
"911"
Returns: 6

From the initial position '5', the finger has to move 2 units to reach the key '9', then 4 units to the key '1' from there. Finally, we can enter the last digit '1' without moving the finger. The return value is thus 2 + 4 + 0 = 6.

1)
"5555555"
Returns: 0

We don't have to move the finger at all, so the result is 0.

2)
"8606335540"
Returns: 16
3)
"8606574276"
Returns: 21
4)
"01928374650123456789"
Returns: 44

Submissions are judged against all 14 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class PhonePad with a public method int fingerMovement(string phoneNumber) · 14 test cases · 2 s / 256 MB per case

Submitting as anonymous