<img alt="" src="https://assets.leetcode.com/uploads/2020/01/02/leetcode_keyboard.png" style="width: 349px; height: 209px;" />
<p>You have a keyboard layout as shown above in the <strong>X-Y</strong> plane, where each English uppercase letter is located at some coordinate.</p>

<ul>
	<li>For example, the letter <code>&#39;A&#39;</code> is located at coordinate <code>(0, 0)</code>, the letter <code>&#39;B&#39;</code> is located at coordinate <code>(0, 1)</code>, the letter <code>&#39;P&#39;</code> is located at coordinate <code>(2, 3)</code> and the letter <code>&#39;Z&#39;</code> is located at coordinate <code>(4, 1)</code>.</li>
</ul>

<p>Given the string <code>word</code>, return <em>the minimum total <strong>distance</strong> to type such string using only two fingers</em>.</p>

<p>The <strong>distance</strong> between coordinates <code>(x<sub>1</sub>, y<sub>1</sub>)</code> and <code>(x<sub>2</sub>, y<sub>2</sub>)</code> is <code>|x<sub>1</sub> - x<sub>2</sub>| + |y<sub>1</sub> - y<sub>2</sub>|</code>.</p>

<p><strong>Note</strong> that the initial positions of your two fingers are considered free so do not count towards your total distance, also your two fingers do not have to start at the first letter or the first two letters.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> word = &quot;CAKE&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;CAKE&quot; is: 
Finger 1 on letter &#39;C&#39; -&gt; cost = 0 
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;C&#39; to letter &#39;A&#39; = 2 
Finger 2 on letter &#39;K&#39; -&gt; cost = 0 
Finger 2 on letter &#39;E&#39; -&gt; cost = Distance from letter &#39;K&#39; to letter &#39;E&#39; = 1 
Total distance = 3
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> word = &quot;HAPPY&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> Using two fingers, one optimal way to type &quot;HAPPY&quot; is:
Finger 1 on letter &#39;H&#39; -&gt; cost = 0
Finger 1 on letter &#39;A&#39; -&gt; cost = Distance from letter &#39;H&#39; to letter &#39;A&#39; = 2
Finger 2 on letter &#39;P&#39; -&gt; cost = 0
Finger 2 on letter &#39;P&#39; -&gt; cost = Distance from letter &#39;P&#39; to letter &#39;P&#39; = 0
Finger 1 on letter &#39;Y&#39; -&gt; cost = Distance from letter &#39;A&#39; to letter &#39;Y&#39; = 4
Total distance = 6
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= word.length &lt;= 300</code></li>
	<li><code>word</code> consists of uppercase English letters.</li>
</ul>

class Solution:
    def minimumDistance(self, word: str) -> int:
        # Predefined coordinates for each letter
        def get_coords(c):
            idx = ord(c) - ord('A')
            return idx // 6, idx % 6
        
        # Calculate distance between two letters
        def dist(c1, c2):
            x1, y1 = get_coords(c1)
            x2, y2 = get_coords(c2)
            return abs(x1 - x2) + abs(y1 - y2)
        
        n = len(word)
        # dp[i][j] means the minimum distance to type word[0...i] with one finger at i and the other at j
        # Initialize with infinity
        dp = [[float('inf')] * 26 for _ in range(n)]
        
        # Initialize the first step
        for j in range(26):
            dp[0][j] = 0  # No cost to start typing the first character
        
        for i in range(1, n):
            current_char = word[i]
            prev_char = word[i - 1]
            
            for j in range(26):
                # The previous finger position can be at any letter
                # Move one finger from previous character to current character
                dp[i][j] = min(dp[i][j], dp[i - 1][j] + dist(prev_char, current_char))
                
                # Move the other finger from j to current character
                prev_char_j = chr(j + ord('A'))
                dp[i][ord(prev_char) - ord('A')] = min(dp[i][ord(prev_char) - ord('A')], dp[i - 1][j] + dist(prev_char_j, current_char))
        
        # The result will be the minimal distance to type the whole word
        result = float('inf')
        last_char_idx = ord(word[-1]) - ord('A')
        
        for j in range(26):
            result = min(result, dp[n - 1][j])
        
        return result

# Example usage:
solution = Solution()
print(solution.minimumDistance("CAKE"))  # Output: 3
print(solution.minimumDistance("HAPPY"))  # Output: 6
17000