In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring" and use the dial to spell a specific keyword to open the door.

Given a string ring that represents the code engraved on the outer ring and another string key that represents the keyword that needs to be spelled, return the minimum number of steps to spell all the characters in the keyword.

Initially, the first character of the ring is aligned at the "12:00" direction. You should spell all the characters in key one by one by rotating the ring clockwise or anticlockwise to make each character of the string key aligned at the "12:00" direction and then by pressing the center button.

At the stage of rotating the ring to spell the key character key[i]:

  • You can rotate the ring clockwise or anticlockwise by one place, which counts as one step.
  • The final purpose of the rotation is to align one of ring's characters at the "12:00" direction, where this character must equal key[i].
  • If the character key[i] has been aligned at the "12:00" direction, press the center button to spell, which also counts as one step.
  • After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.

Input & Output

Example 1 — Basic Case
$ Input: ring = "godding", key = "gd"
Output: 4
💡 Note: Start at 'g'(0), press button (1 step). Rotate to 'd'(2): min(2 clockwise, 5 anticlockwise) = 2 steps. Press button (1 step). Total: 1 + 2 + 1 = 4 steps.
Example 2 — Single Character
$ Input: ring = "godding", key = "g"
Output: 1
💡 Note: Already at 'g'(0), just press button. Total: 1 step.
Example 3 — Multiple Choices
$ Input: ring = "abcde", key = "ae"
Output: 3
💡 Note: Start at 'a'(0), press button (1 step). Rotate to 'e'(4): min(4 clockwise, 1 anticlockwise) = 1 step. Press button (1 step). Total: 1 + 1 + 1 = 3 steps.

Constraints

  • 1 ≤ ring.length, key.length ≤ 100
  • ring and key consist of only lower case English letters
  • It's guaranteed that all characters in key exist in ring

Visualization

Tap to expand
Freedom Trail: Minimum Steps to Spell "gd"g(0)o(1)d(2)d(3)i(4)n(5)g(6)Ring: "godding"2 steps clockwiseStep-by-step:1. Start at g(0), press button: 1 step2. Rotate to d(2): min(2 clockwise, 5 anticlockwise) = 2 steps3. Press button for d: 1 stepTotal: 1 + 2 + 1 = 4 stepsKey InsightMultiple positions for same character:Choose position minimizing total steps
Understanding the Visualization
1
Input Setup
Ring "godding" with characters at positions 0-6, key "gd" to spell
2
Rotation Strategy
Calculate clockwise vs anticlockwise distances, choose minimum
3
Optimal Path
Start at g(0), rotate to d(2), total 4 steps including button presses
Key Takeaway
🎯 Key Insight: When characters appear multiple times in the ring, use dynamic programming to find the optimal sequence of positions that minimizes total rotation steps.
Asked in
Google 15 Microsoft 12 Amazon 8 Facebook 6
28.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen