Fewest Steps to Spell a Word on a Ring Dial
A circular dial has letters around its rim and a fixed pointer at 12 o'clock. To spell a key word, you rotate letters under the pointer and press; minimize total rotations plus presses.
Problem
Given a string ring (letters arranged clockwise on a dial, pointer initially at index 0) and a string key, return the minimum number of steps to spell key. Each step is either a one-position clockwise/counterclockwise rotation of the ring, or a press of the button. To spell a character, the matching ring letter must be at the pointer (index 0 position relative to rotation), then pressed (1 step).
Input
Two strings ring and key consisting of lowercase letters.
Output
An integer: the minimum total steps (rotations + presses).
Constraints
- 1 <= ring.length, key.length <= 100
- ring and key contain only lowercase letters.
- key is guaranteed spellable from ring's letters.
Examples
Example 1
Input
ring = "godding", key = "gd"
Output
4
Rotate to \'g\' (press), then rotate to a \'d\' and press; total 4 steps.
Example 2
Input
ring = "godding", key = "godding"
Output
13
Spelling the whole ring word back costs 13 steps.