1320. Minimum Distance to Type a Word Using Two Fingers

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.

Rust Solution

struct Solution;

impl Solution {
    fn minimum_distance(word: String) -> i32 {
        let n = word.len();
        let s: Vec<i32> = word.bytes().map(|b| (b - b'A') as i32).collect();
        let mut memo: Vec<Vec<Vec<i32>>> = vec![vec![vec![-1; 27]; 27]; n];
        Self::dp(0, 26, 26, &mut memo, &s, n)
    }

    fn dp(
        start: usize,
        f1: i32,
        f2: i32,
        memo: &mut Vec<Vec<Vec<i32>>>,
        s: &[i32],
        n: usize,
    ) -> i32 {
        if start == n {
            0
        } else {
            if memo[start][f1 as usize][f2 as usize] != -1 {
                return memo[start][f1 as usize][f2 as usize];
            }
            let mut res = std::i32::MAX;
            let g = s[start];
            res = res.min(Self::dp(start + 1, g, f2, memo, s, n) + Self::dist(f1, g));
            res = res.min(Self::dp(start + 1, f1, g, memo, s, n) + Self::dist(f2, g));
            memo[start][f1 as usize][f2 as usize] = res;
            res
        }
    }

    fn dist(f: i32, g: i32) -> i32 {
        if f == 26 {
            0
        } else {
            (f / 6 - g / 6).abs() + (f % 6 - g % 6).abs()
        }
    }
}

#[test]
fn test() {
    let word = "CAKE".to_string();
    let res = 3;
    assert_eq!(Solution::minimum_distance(word), res);
    let word = "HAPPY".to_string();
    let res = 6;
    assert_eq!(Solution::minimum_distance(word), res);
    let word = "NEW".to_string();
    let res = 3;
    assert_eq!(Solution::minimum_distance(word), res);
    let word = "YEAR".to_string();
    let res = 7;
    assert_eq!(Solution::minimum_distance(word), res);
}

Having problems with this solution? Click here to submit an issue on github.