Skip to content
← All posts

Minimum Time to Type Word Using Special Typewriter: Greedy Circular Distance

5 min read
leetcodeproblemeasystringsgreedy

LeetCode 1974. Minimum Time to Type Word Using Special Typewriter presents a circular dial with 26 lowercase letters arranged in order. A pointer starts at 'a'. Each second, you can either move the pointer one position clockwise, one position counterclockwise, or type the character the pointer is currently on. Given a string word, return the minimum number of seconds to type the entire word.

abcdefghijklmnopqrstuvwxyzword = "bza"pointer (starts at 'a')target letters
A circular dial with 26 letters. The pointer starts at 'a' and can move clockwise or counterclockwise. Each move costs 1 second, and typing a character costs 1 second.

Why this problem matters

This problem is a clean introduction to circular distance calculations. The idea of choosing the shorter of two paths around a ring shows up in problems involving modular arithmetic, rotational arrays, and clock-based geometry. It also reinforces the greedy principle: when each decision is independent of future decisions, you can make the locally optimal choice at every step without worrying about global consequences.

If you have solved Angle Between Hands of a Clock, you have already seen the "min of two arcs" idea. This problem strips away the math and gives you the pattern in its simplest form.

The key insight

The 26 letters form a circle. To move from one letter to another, you can go clockwise or counterclockwise. The clockwise distance from letter a to letter b is (ord(b) - ord(a)) % 26. The counterclockwise distance is 26 - clockwise_distance. You always pick whichever is smaller.

Since the choice for each character is completely independent of the others, you just greedily pick the shorter direction at every step and add 1 second for the type action. Sum everything up and you have your answer.

The solution

def min_time_to_type(word: str) -> int:
    total = 0
    curr = 0
    for ch in word:
        target = ord(ch) - ord('a')
        diff = abs(target - curr)
        total += min(diff, 26 - diff) + 1
        curr = target
    return total

The variable curr tracks the pointer's current position as an integer from 0 to 25. For each character in the word, we compute diff, the raw distance between the current position and the target. The circular distance is min(diff, 26 - diff). We add 1 for the typing action, update the pointer, and move on.

That is the entire solution. No data structures, no sorting, no edge-case branches. Just one loop and one formula.

The circular distance formula min(diff, n - diff) works for any ring of size n. You will see it in problems involving circular arrays, clock positions, and modular arithmetic. Memorize the pattern: compute the raw difference, then take the minimum of that difference and n minus the difference.

Visual walkthrough

Let's trace through word = "bza" with the pointer starting at 'a'. At each step, we compare clockwise and counterclockwise distances and pick the shorter path.

Tracing minTimeToType("bza"), starting at 'a':

Step 1: Type 'b'

ab
from:'a'to:'b'
clockwise:1 steps
counterclockwise:25 steps
best:min(1, 25) = 1 (clockwise)
cost:1 move + 1 type = 2
running total = 2

The pointer is at 'a'. Moving clockwise 1 step to 'b' is much shorter than going 25 steps counterclockwise. Move 1 + type 1 = 2 seconds.

Step 2: Type 'z'

bz
from:'b'to:'z'
clockwise:24 steps
counterclockwise:2 steps
best:min(24, 2) = 2 (counterclockwise)
cost:2 move + 1 type = 3
running total = 5

From 'b', going counterclockwise 2 steps reaches 'z' (b -> a -> z). Much better than 24 steps clockwise. Move 2 + type 1 = 3 seconds.

Step 3: Type 'a'

za
from:'z'to:'a'
clockwise:1 steps
counterclockwise:25 steps
best:min(1, 25) = 1 (clockwise)
cost:1 move + 1 type = 2
running total = 7

From 'z', clockwise 1 step wraps around to 'a'. Move 1 + type 1 = 2 seconds. Final answer: 7.

Result: 2 + 3 + 2 = 7 seconds

At each step, we picked the shorter of the two directions around the circle. This greedy choice is always optimal because the two directions are independent for each character.

Notice how the direction flips between steps. For 'b', clockwise is shorter. For 'z', counterclockwise wins because 'z' is only 2 steps back from 'b' (b, a, z). For the final 'a', clockwise wraps from 'z' to 'a' in a single step. The greedy choice at each step is trivially correct because no future character can change which direction is shorter right now.

The pointer always ends at the last character you typed. This means the order of characters in the word matters. The same set of characters in a different order could produce a completely different total cost.

Complexity analysis

ApproachTimeSpace
GreedyO(n)O(1)

We iterate through the word once, doing constant work per character. No extra memory is needed beyond a couple of integer variables. This is optimal since you must read every character at least once.

Edge cases

  • Single character "a": The pointer starts at 'a', so the move cost is 0 and the type cost is 1. Total = 1.
  • Single character "z": Clockwise distance is 25, counterclockwise is 1. Total = 1 + 1 = 2.
  • Repeated characters "aaa": The pointer never moves. Each character costs 1 second to type. Total = 3.
  • Adjacent characters "abc": Each move is 1 step clockwise plus 1 type. Total = 6.
  • Opposite side of the dial: From 'a' to 'n' is 13 either way, so min(13, 13) = 13.

The building blocks

1. Circular distance

Given two positions on a ring of size n, the shortest distance is:

diff = abs(a - b)
circular_dist = min(diff, n - diff)

This pattern appears whenever you need the shortest path between two points on a cycle. Clock problems, rotational arrays, and ring buffers all use it.

2. Greedy accumulation

When each local decision is independent and locally optimal choices lead to a globally optimal result, you accumulate the cost in a single pass:

total = 0
for item in sequence:
    total += best_local_cost(item)
return total

There is no backtracking, no memoization, no dynamic programming. Each step contributes its cost and you move on. This works here because the optimal direction for character i does not depend on what characters come later.

Common mistakes

1. Forgetting the +1 for typing. Moving to a character is not enough. You also spend 1 second pressing the key. Every character contributes at least 1 second, even if the pointer is already on it.

2. Not using modular distance. Computing abs(target - curr) gives the raw linear distance, but on a circle of 26 letters you need min(diff, 26 - diff). Without the min, you might take the long way around for letters near the wrap point (like going from 'b' to 'z').

3. Forgetting to update the pointer. After typing a character, the pointer stays where it is. If you forget curr = target, every distance calculation will be measured from 'a' instead of from the previous character.

4. Using character subtraction without abs. The difference target - curr can be negative if you are moving backward in the alphabet. Always take the absolute value before applying the circular distance formula.

From understanding to recall

This problem has a short solution, but the underlying pattern (circular distance plus greedy accumulation) shows up in many places. The risk is not that you will struggle with this exact problem. It is that you will see a harder problem that uses circular distance as one component and fail to recognize it under pressure.

Spaced repetition solves this. CodeBricks isolates the circular distance formula and the greedy accumulation pattern as separate building blocks. You practice writing each one from scratch at increasing intervals. After a few review cycles, min(diff, n - diff) is something you type without thinking. When a problem like "minimum rotations" or "shortest distance in a circular array" appears, the building block is already automatic.

Related posts

CodeBricks breaks the special typewriter problem into its circular distance and greedy accumulation building blocks, then drills them independently with spaced repetition. You type each piece from scratch until the pattern is automatic. When a circular distance question shows up in your interview, you do not hesitate. You just write it.