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.
Input: word ="abc"Output: 5Explanation: The characters are printed as follows:- Type the character 'a'in1 second since the pointer is initially on 'a'.- Move the pointer clockwise to 'b'in1 second.- Type the character 'b'in1 second.- Move the pointer clockwise to 'c'in1 second.- Type the character 'c'in1 second.
Input: word ="bza"Output: 7Explanation: The characters are printed as follows:- Move the pointer clockwise to 'b'in1 second.- Type the character 'b'in1 second.- Move the pointer counterclockwise to 'z'in2 seconds.- Type the character 'z'in1 second.- Move the pointer clockwise to 'a'in1 second.- Type the character 'a'in1 second.
Input: word ="zjpc"Output: 34Explanation:
The characters are printed as follows:- Move the pointer counterclockwise to 'z'in1 second.- Type the character 'z'in1 second.- Move the pointer clockwise to 'j'in10 seconds.- Type the character 'j'in1 second.- Move the pointer clockwise to 'p'in6 seconds.- Type the character 'p'in1 second.- Move the pointer counterclockwise to 'c'in13 seconds.- Type the character 'c'in1 second.
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.