Minimum Time to Type Word Using Special Typewriter - 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 the minimum number of seconds to type out the characters in word.

Input & Output

Example 1 — Basic Case
$ Input: word = "abc"
Output: 5
💡 Note: Start at 'a': 0 seconds to move + 1 to type = 1 second. Move to 'b': 1 second + 1 to type = 2 seconds. Move to 'c': 1 second + 1 to type = 2 seconds. Total: 1 + 2 + 2 = 5 seconds.
Example 2 — Wraparound Case
$ Input: word = "bza"
Output: 7
💡 Note: Start at 'a', move to 'b': 1 + 1 = 2 seconds. Move to 'z': counterclockwise 1 step + 1 to type = 2 seconds. Move to 'a': clockwise 1 step + 1 to type = 2 seconds. Total: 2 + 2 + 2 = 6 seconds... Wait, let me recalculate: 'a' to 'b' = 1+1=2, 'b' to 'z' = min(24,2)=2, so 2+1=3, 'z' to 'a' = min(1,25)=1, so 1+1=2. Total = 2+3+2=7.
Example 3 — Single Character
$ Input: word = "a"
Output: 1
💡 Note: Already at 'a', so 0 seconds to move + 1 second to type = 1 second total.

Constraints

  • 1 ≤ word.length ≤ 100
  • word consists of lowercase English letters.

Visualization

Tap to expand
Circular Typewriter: word = "bza"abzLetters arranged in circleStep 1: a → b (1 move + 1 type = 2s)Step 2: b → z (2 moves + 1 type = 3s)Step 3: z → a (1 move + 1 type = 2s)Total: 2 + 3 + 2 = 7 seconds
Understanding the Visualization
1
Input
Word to type on circular keyboard starting at 'a'
2
Process
For each character, choose shorter clockwise/counterclockwise path
3
Output
Minimum total seconds needed
Key Takeaway
🎯 Key Insight: Always choose the shorter path around the circle for each character transition
Asked in
Google 15 Amazon 12 Microsoft 8
25.4K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen