# Minimum Distance to Type a Word Using Two Fingers in C++

C++Server Side ProgrammingProgramming

Suppose we have a keyboard layout like below −

 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Where each English uppercase letter is located at some coordinate, as an example, the letter A is placed at (0,0), the letter B is placed at (0,1), the letter P is placed at (2,3) and the letter Z is placed at (4,1). Now if we have a word, we have to find the minimum total distance to type such string using only two fingers. The distance between two places (x1,y1) and (x2,y2) is |x1 - x2| + |y1 - y2|. And we can start from any position on the keyboard.

So, if the input is like "HAPPY", then the output will be 6, as Start with H, so cost is 0, then A, so cost from H to A is 2, then P, so cost is 0, then again P, cost is 0, and finally Y, so cost from P to Y is 4, so the total cost is 6 + 4 = 10.

To solve this, we will follow these steps −

• Define one map memo

• Define a function getHash(), this will take a, b, c, d, e,

• temp := 0

• while a is non-zero, do −

• temp := temp * 10 + a mod 10, a := a / 10

• while b is non-zero, do −

• temp := temp * 10 + b mod 10, b := b / 10

• while c is non-zero, do −

• temp := temp * 10 + d mod 10, d := d / 10

• while e is non-zero, do

• temp := temp * 10 + e mod 10, e := e / 10

• return temp

• Define one method getXY(), this will take c

• Define one pair ret

• a := c - ASCII of 'A'

• ret.second := a mod 6

• ret.first := a / 6

• return ret

• Define a function getDist(), this will take a, b, c, d,

• if a is same as -1 and b is same as -1, then −

• return 0

• return |(b - d) + |a - c||

• Define a function solve(), this will take x1, y1, x2, y2, word, idx,

• if idx is same as size of word, then −

• return 0

• state := getHash(x1 + 2, y1 + 2, x2 + 2, y2 + 2, idx + 2)

• if state is in memo then −

• return memo[state]

• Define one pair temp := getXY(word[idx])

• ans := 0

• A := getDist(x1, y1, temp.first, temp.second + solve(temp.first, temp.second, x2, y2, word, idx + 1))

• B := getDist(x2, y2, temp.first, temp.second + solve(x1, y1, temp.first, temp.second, word, idx + 1))

• ans := minimum of A and B

• return memo[state] = ans

• From the main method do the following −

• return solve(-1, -1, -1, -1, word, 0)

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
map<int, int> memo;
int getHash(int a, int b, int c, int d, int e){
int temp = 0;
while (a) {
temp = temp * 10 + a % 10;
a /= 10;
}
while (b) {
temp = temp * 10 + b % 10;
b /= 10;
}
while (c) {
temp = temp * 10 + c % 10;
c /= 10;
}
while (d) {
temp = temp * 10 + d % 10;
d /= 10;
}
while (e) {
temp = temp * 10 + e % 10;
e /= 10;
}
return temp;
}
pair<int, int> getXY(char c){
pair<int, int> ret;
int a = c - 'A';
ret.second = a % 6;
ret.first = a / 6;
return ret;
}
int getDist(int a, int b, int c, int d){
if (a == -1 && b == -1)
return 0;
return abs(b - d) + abs(a - c);
}
int solve(int x1, int y1, int x2, int y2, string word, int idx){
if (idx == word.size())
return 0;
int state = getHash(x1 + 2, y1 + 2, x2 + 2, y2 + 2, idx + 2);
if (memo.find(state) != memo.end())
return memo[state];
pair<int, int> temp = getXY(word[idx]);
int ans = 0;
int A = getDist(x1, y1, temp.first, temp.second) +
solve(temp.first, temp.second, x2, y2, word, idx + 1);
int B = getDist(x2, y2, temp.first, temp.second) + solve(x1,
y1, temp.first, temp.second, word, idx + 1);
ans = min(A, B);
return memo[state] = ans;
}
int minimumDistance(string word){
memo.clear();
return solve(-1, -1, -1, -1, word, 0);
}
};
main(){
Solution ob;;
cout << (ob.minimumDistance("HELLO"));
}

## Input

"HELLO"

## Output

4