
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum Distance to Type a Word Using Two Fingers in C++
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
#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
- Related Articles
- Program to find minimum distance of two given words in a text in Python
- Find the minimum distance between two numbers in C++
- Program to find the minimum edit distance between two strings in C++
- Program to find minimum distance to the target element using Python
- Minimum Unique Word Abbreviation in C++
- Minimum Word Break Problem in C++
- Shortest Word Distance II in C++
- Shortest Word Distance III in C++
- Word formation using concatenation of two dictionary words in C++
- How to get rid of swollen fingers in winters?
- The distance between the two stations is 216 km. A bus takes 4 hours to cover that distance. Calculate the average speed of the word in km/hour?
- Finding distance between two points in a 2-D plane using JavaScript
- Calculate distance and duration between two places using google distance matrix API in Python?
- How to match a word in python using Regular Expression?
- How to reverse a given string word by word instead of letters using C#?
