---
# System prepended metadata

title: Leetcode 1974. Minimum Time to Type Word Using Special Typewriter
tags: [Leetcode]

---

# Leetcode 1974. Minimum Time to Type Word Using Special Typewriter
[1974. Minimum Time to Type Word Using Special Typewriter](https://leetcode.com/problems/minimum-time-to-type-word-using-special-typewriter/description/) (<font color=#00AF9B>Easy</font> 74.9%) 
<!-- 
(<font color=#00AF9B>Easy</font> 53.8%) 
(<font color=#FFB800>Medium</font> 39.6%)
(<font color=#FF375F>Hard</font>)
 -->
    
- 限制 :

> `1 <= word.length <= 100`

> `word` consists of lowercase English letters.
- Solution
    就很簡單地找出哪一個比較少的就好了，最後加起來數字要對就好。

    - 時間複雜度: $O(n)$
    - 空間複雜度: $O(1)$

- 程式碼
    ```c++=
    class Solution {
    public:
        int minTimeToType(string word) {
            int result = 0;
            char initial_char = 'a';
            for (auto& chr : word)
            {
                if (chr > initial_char)
                    result += (min(chr - initial_char, 26 + initial_char - chr) + 1);
                else
                    result += (min(26 + chr - initial_char, initial_char - chr) + 1);
                initial_char = chr;
            }
            return result;
        }
    };
    ```
