Problem

There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'.

Each second, you may perform one of the following operations:

  • Move the pointer one character counterclockwise or clockwise.
  • Type the character the pointer is currently on.

Given a string word, return theminimum number of seconds to type out the characters in word.

Examples

Example 1

1
2
3
4
5
6
7
8
Input: word = "abc"
Output: 5
Explanation: The characters are printed as follows:
- Type the character 'a' in 1 second since the pointer is initially on 'a'.
- Move the pointer clockwise to 'b' in 1 second.
- Type the character 'b' in 1 second.
- Move the pointer clockwise to 'c' in 1 second.
- Type the character 'c' in 1 second.

Example 2

1
2
3
4
5
6
7
8
9
Input: word = "bza"
Output: 7
Explanation: The characters are printed as follows:
- Move the pointer clockwise to 'b' in 1 second.
- Type the character 'b' in 1 second.
- Move the pointer counterclockwise to 'z' in 2 seconds.
- Type the character 'z' in 1 second.
- Move the pointer clockwise to 'a' in 1 second.
- Type the character 'a' in 1 second.

Example 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Input: word = "zjpc"
Output: 34
Explanation:
The characters are printed as follows:
- Move the pointer counterclockwise to 'z' in 1 second.
- Type the character 'z' in 1 second.
- Move the pointer clockwise to 'j' in 10 seconds.
- Type the character 'j' in 1 second.
- Move the pointer clockwise to 'p' in 6 seconds.
- Type the character 'p' in 1 second.
- Move the pointer counterclockwise to 'c' in 13 seconds.
- Type the character 'c' in 1 second.

Constraints

  • 1 <= word.length <= 100
  • word consists of lowercase English letters.

Solution

Method 1 – Greedy Circular Distance

Intuition

To minimize the time, always move the pointer the shortest way (clockwise or counterclockwise) to the next character. For each character, the cost is the minimum of the clockwise and counterclockwise distance from the current pointer position, plus 1 second to type.

Approach

  1. Start with the pointer at ‘a’.
  2. For each character in the word:
    • Calculate the clockwise and counterclockwise distance from the current pointer position.
    • Add the minimum distance to the answer.
    • Add 1 second for typing the character.
    • Move the pointer to the current character.
  3. Return the total time.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int minTimeToType(string word) {
        int ans = 0, curr = 'a';
        for (char ch : word) {
            int dist = abs(ch - curr);
            ans += min(dist, 26 - dist) + 1;
            curr = ch;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
func minTimeToType(word string) int {
    ans, curr := 0, 'a'
    for _, ch := range word {
        dist := abs(int(ch) - int(curr))
        if dist > 13 {
            dist = 26 - dist
        }
        ans += dist + 1
        curr = ch
    }
    return ans
}
func abs(x int) int {
    if x < 0 {
        return -x
    }
    return x
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public int minTimeToType(String word) {
        int ans = 0, curr = 'a';
        for (char ch : word.toCharArray()) {
            int dist = Math.abs(ch - curr);
            ans += Math.min(dist, 26 - dist) + 1;
            curr = ch;
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun minTimeToType(word: String): Int {
        var ans = 0
        var curr = 'a'
        for (ch in word) {
            val dist = kotlin.math.abs(ch - curr)
            ans += minOf(dist, 26 - dist) + 1
            curr = ch
        }
        return ans
    }
}
1
2
3
4
5
6
7
8
9
class Solution:
    def minTimeToType(self, word: str) -> int:
        ans: int = 0
        curr: str = 'a'
        for ch in word:
            dist: int = abs(ord(ch) - ord(curr))
            ans += min(dist, 26 - dist) + 1
            curr = ch
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
impl Solution {
    pub fn min_time_to_type(word: String) -> i32 {
        let mut ans = 0;
        let mut curr = b'a';
        for &ch in word.as_bytes() {
            let dist = (ch as i32 - curr as i32).abs();
            ans += std::cmp::min(dist, 26 - dist) + 1;
            curr = ch;
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    minTimeToType(word: string): number {
        let ans = 0, curr = 'a'.charCodeAt(0);
        for (let i = 0; i < word.length; i++) {
            const ch = word.charCodeAt(i);
            const dist = Math.abs(ch - curr);
            ans += Math.min(dist, 26 - dist) + 1;
            curr = ch;
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of word, since we process each character once.
  • 🧺 Space complexity: O(1), only a few variables are used regardless of input size.