#K14731. Minimum Typing Time
Minimum Typing Time
Minimum Typing Time
You are given a special keyboard arranged in a single row with the lowercase English letters in order:
\(K = \text{'abcdefghijklmnopqrstuvwxyz'}\)
Your finger starts at the key 'a'
(i.e. the first character in \(K\)). To type a given string \(s\), you need to move your finger from one key to another. The time taken to move from one key to the other is equal to the absolute difference of their positions in \(K\). For example, moving from 'a'
to 'c'
takes \(|3-1| = 2\) units of time.
Your task is to compute the total time required to type out the string \(s\) optimally.
Example:
- If
s = "cba"
, then the time taken is \(|3-1|+|2-3| = 2+1 = 3\). Note that the expected output in our example is 4 based on 1-indexed positions. Therefore, use 1-indexed positions: position of 'a' is 1, 'b' is 2, ..., 'z' is 26. - For
s = "zebra"
, the total time is computed as \(|26-1|+|5-26|+|2-5|+|18-2|+|1-18| = 25+21+3+16+17 = 82\).
Note: If the string is empty, the output should be 0.
inputFormat
The input is provided via stdin and consists of a single line containing the string \(s\) (which may be empty). The string \(s\) comprises only lowercase English letters.
Example:
a
outputFormat
Output the minimum time required to type the string \(s\) (an integer) to stdout.
Example:
0## sample
cba
4