Minimum Time to Type Word Using Special Typewriter

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.

Example 1
Inputword = "abc"
Output5
The characters are typed by typing 'a', moving to and typing 'b', then moving to and typing 'c', for a total of 5 seconds.
Example 2
Inputword = "bza"
Output7
The characters are typed by moving to and typing 'b', moving counterclockwise to and typing 'z', then moving clockwise to and typing 'a', for a total of 7 seconds.

Constraints

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

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate