##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1320.html

# 1320. Minimum Distance to Type a Word Using Two Fingers (Hard) You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for example, the letter A is located at coordinate (0,0), the letter B is located at coordinate (0,1), the letter P is located at coordinate (2,3) and the letter Z is located at coordinate (4,1).

Given the string word, return the minimum total distance to type such string using only two fingers. The distance between coordinates (x1,y1) and (x2,y2) is |x1 - x2| + |y1 - y2|

Note that the initial positions of your two fingers are considered free so don't count towards your total distance, also your two fingers do not have to start at the first letter or the first two letters.

Example 1:

Input: word = "CAKE"
Output: 3
Explanation:
Using two fingers, one optimal way to type "CAKE" is:
Finger 1 on letter 'C' -> cost = 0
Finger 1 on letter 'A' -> cost = Distance from letter 'C' to letter 'A' = 2
Finger 2 on letter 'K' -> cost = 0
Finger 2 on letter 'E' -> cost = Distance from letter 'K' to letter 'E' = 1
Total distance = 3


Example 2:

Input: word = "HAPPY"
Output: 6
Explanation:
Using two fingers, one optimal way to type "HAPPY" is:
Finger 1 on letter 'H' -> cost = 0
Finger 1 on letter 'A' -> cost = Distance from letter 'H' to letter 'A' = 2
Finger 2 on letter 'P' -> cost = 0
Finger 2 on letter 'P' -> cost = Distance from letter 'P' to letter 'P' = 0
Finger 1 on letter 'Y' -> cost = Distance from letter 'A' to letter 'Y' = 4
Total distance = 6


Example 3:

Input: word = "NEW"
Output: 3


Example 4:

Input: word = "YEAR"
Output: 7


Constraints:

• 2 <= word.length <= 300
• Each word[i] is an English uppercase letter.

Related Topics:
Dynamic Programming

## Solution 1. DP

// OJ: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/
// Time: O(N)
// Space: O(1)
// Ref: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/477652/JavaC%2B%2BPython-1D-DP-O(1)-Space
class Solution {
int d(int a, int b) {
return abs(a / 6 - b / 6) + abs(a % 6 - b % 6);
}
public:
int minimumDistance(string word) {
vector<int> dp(26);
int res = 0, save = 0, n = word.size();
for (int i = 0; i < n - 1; ++i) {
int b = word[i] - 'A', c = word[i + 1] - 'A';
for (int a = 0; a < 26; ++a)
dp[b] = max(dp[b], dp[a] + d(b, c) - d(a, c));
save = max(save, dp[b]);
res += d(b, c);
}
return res - save;
}
};

• class Solution {
public int minimumDistance(String word) {
int length = word.length();
int[][] dp = new int[length][length];
int[][] distances = new int[length][length];
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++)
distances[i][j] = distance(word.charAt(i) - 'A', word.charAt(j) - 'A');
}
for (int i = 1; i < length; i++) {
for (int j = 0; j < length; j++)
dp[i][j] = Integer.MAX_VALUE;
for (int j = 0; j < i - 1; j++) {
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j] + distances[i - 1][i]);
dp[i][i - 1] = Math.min(dp[i][i - 1], dp[i - 1][j] + distances[j][i]);
}
dp[i][i - 1] = Math.min(dp[i][i - 1], dp[i - 1][i - 1]);
dp[i][i] = dp[i - 1][i - 1] + distances[i - 1][i];
}
int minDistance = Integer.MAX_VALUE;
for (int i = 0; i < length - 1; i++)
minDistance = Math.min(minDistance, dp[length - 1][i]);
return minDistance;
}

public int distance(int position1, int position2) {
int x1 = position1 / 6, y1 = position1 % 6;
int x2 = position2 / 6, y2 = position2 % 6;
return Math.abs(x2 - x1) + Math.abs(y2 - y1);
}
}

############

class Solution {
public int minimumDistance(String word) {
int n = word.length();
final int inf = 1 << 30;
int[][][] f = new int[n];
for (int[][] g : f) {
for (int[] h : g) {
Arrays.fill(h, inf);
}
}
for (int j = 0; j < 26; ++j) {
f[word.charAt(0) - 'A'][j] = 0;
f[j][word.charAt(0) - 'A'] = 0;
}
for (int i = 1; i < n; ++i) {
int a = word.charAt(i - 1) - 'A';
int b = word.charAt(i) - 'A';
int d = dist(a, b);
for (int j = 0; j < 26; ++j) {
f[i][b][j] = Math.min(f[i][b][j], f[i - 1][a][j] + d);
f[i][j][b] = Math.min(f[i][j][b], f[i - 1][j][a] + d);
if (j == a) {
for (int k = 0; k < 26; ++k) {
int t = dist(k, b);
f[i][b][j] = Math.min(f[i][b][j], f[i - 1][k][a] + t);
f[i][j][b] = Math.min(f[i][j][b], f[i - 1][a][k] + t);
}
}
}
}
int ans = inf;
for (int j = 0; j < 26; ++j) {
ans = Math.min(ans, f[n - 1][j][word.charAt(n - 1) - 'A']);
ans = Math.min(ans, f[n - 1][word.charAt(n - 1) - 'A'][j]);
}
return ans;
}

private int dist(int a, int b) {
int x1 = a / 6, y1 = a % 6;
int x2 = b / 6, y2 = b % 6;
return Math.abs(x1 - x2) + Math.abs(y1 - y2);
}
}

• // OJ: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/
// Time: O(N)
// Space: O(1)
// Ref: https://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/477652/JavaC%2B%2BPython-1D-DP-O(1)-Space
class Solution {
int d(int a, int b) {
return abs(a / 6 - b / 6) + abs(a % 6 - b % 6);
}
public:
int minimumDistance(string word) {
vector<int> dp(26);
int res = 0, save = 0, n = word.size();
for (int i = 0; i < n - 1; ++i) {
int b = word[i] - 'A', c = word[i + 1] - 'A';
for (int a = 0; a < 26; ++a)
dp[b] = max(dp[b], dp[a] + d(b, c) - d(a, c));
save = max(save, dp[b]);
res += d(b, c);
}
return res - save;
}
};

• class Solution:
def minimumDistance(self, word: str) -> int:
def dist(a: int, b: int) -> int:
x1, y1 = divmod(a, 6)
x2, y2 = divmod(b, 6)
return abs(x1 - x2) + abs(y1 - y2)

n = len(word)
f = [[[inf] * 26 for _ in range(26)] for _ in range(n)]
for j in range(26):
f[ord(word) - ord('A')][j] = 0
f[j][ord(word) - ord('A')] = 0
for i in range(1, n):
a, b = ord(word[i - 1]) - ord('A'), ord(word[i]) - ord('A')
d = dist(a, b)
for j in range(26):
f[i][b][j] = min(f[i][b][j], f[i - 1][a][j] + d)
f[i][j][b] = min(f[i][j][b], f[i - 1][j][a] + d)
if j == a:
for k in range(26):
t = dist(k, b)
f[i][b][j] = min(f[i][b][j], f[i - 1][k][a] + t)
f[i][j][b] = min(f[i][j][b], f[i - 1][a][k] + t)
a = min(f[n - 1][ord(word[-1]) - ord('A')])
b = min(f[n - 1][j][ord(word[-1]) - ord('A')] for j in range(26))
return int(min(a, b))


• func minimumDistance(word string) int {
n := len(word)
f := make([]int, n)
const inf = 1 << 30
for i := range f {
for j := range f[i] {
for k := range f[i][j] {
f[i][j][k] = inf
}
}
}
for j := range f {
f[word-'A'][j] = 0
f[j][word-'A'] = 0
}
dist := func(a, b int) int {
x1, y1 := a/6, a%6
x2, y2 := b/6, b%6
return abs(x1-x2) + abs(y1-y2)
}
for i := 1; i < n; i++ {
a, b := int(word[i-1]-'A'), int(word[i]-'A')
d := dist(a, b)
for j := 0; j < 26; j++ {
f[i][b][j] = min(f[i][b][j], f[i-1][a][j]+d)
f[i][j][b] = min(f[i][j][b], f[i-1][j][a]+d)
if j == a {
for k := 0; k < 26; k++ {
t := dist(k, b)
f[i][b][j] = min(f[i][b][j], f[i-1][k][a]+t)
f[i][j][b] = min(f[i][j][b], f[i-1][a][k]+t)
}
}
}
}
ans := inf
for j := 0; j < 26; j++ {
ans = min(ans, f[n-1][word[n-1]-'A'][j])
ans = min(ans, f[n-1][j][word[n-1]-'A'])
}
return ans
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}