Problem 527
Minimum Time to Type Word Using Special Typewriter
Easy · 10 pts · String, Greedy
Problem
A typewriter has the letters a to z arranged in a circle, with a pointer that starts at the letter a. Each second you may move the pointer one letter clockwise or counterclockwise, or type the letter under the pointer. Find the minimum number of seconds needed to type a given word. Moving between two letters takes min(d, 26 - d) seconds, where d is the distance between them in the alphabet, and every letter costs one more second to type.
Input
A single word W of lowercase English letters.
Output
Print the minimum number of seconds.
Constraints
1 <= |W| <= 100000
Example input
abc
Example output
5
Explanation
For abc: type a (1 second), move to b and type it (2 seconds), move to c and type it (2 seconds). That is 5 seconds in total.