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 - Dynamic Programming INPUT 12:00 g o d d i n g PRESS ring = "godding" key = "gd" Index: 0=g, 1=o, 2=d, 3=d 4=i, 5=n, 6=g ALGORITHM STEPS 1 Define DP State dp[i][j] = min steps for key[i:] with ring[j] at 12:00 2 Spell 'g' (key[0]) Already at 12:00! Steps: 1 (press button only) 3 Spell 'd' (key[1]) Clockwise: 0-->2 = 2 steps Counter: 0-->6-->5..= 5 steps Min = 2, + 1 press = 3 4 Calculate Total Total = 1 + 3 = 4 steps DP Transitions pos 0(g): rotate=0, press=1 pos 2(d): rotate=2, press=1 Total: 0+1+2+1 = 4 FINAL RESULT g o d d i n g 2 steps Output 4 Step breakdown: 'g': 0 rotate + 1 press 'd': 2 rotate + 1 press Total: 4 steps Key Insight: DP optimizes by tracking minimum steps from each ring position. For each key character, compare clockwise vs counter-clockwise rotations: min(pos, n-pos) where n = ring length. Time: O(key.length * ring.length^2) | Space: O(key.length * ring.length) TutorialsPoint - Freedom Trail | Dynamic Programming Approach
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