Freedom Trail Ring - Minimize Steps to Spell Keyword

Imagine you're playing Fallout 4 and standing before the iconic Freedom Trail Ring - a circular dial with letters engraved around its edge. Your mission: spell out a secret keyword by rotating the ring and pressing the center button.

๐ŸŽฏ The Challenge:
- You have a ring string representing letters arranged in a circle
- You need to spell each character of a key string in order
- Initially, ring[0] is aligned at the "12:00" position
- For each character in key:
  โ€ข Rotate the ring clockwise or counterclockwise (1 step per position)
  โ€ข Press the center button when the target character is at 12:00 (1 step)

Goal: Return the minimum total steps needed to spell the entire keyword.

Example: If ring = "godding" and key = "gd":
- 'g' is already at position 0, so press button (1 step)
- 'd' is at position 1, rotate 1 step clockwise + press (2 steps)
- Total: 3 steps

Input & Output

example_1.py โ€” Basic Case
$ Input: ring = "godding", key = "gd"
โ€บ Output: 4
๐Ÿ’ก Note: Start at 'g'(0) โ†’ press button (1 step) โ†’ rotate to 'd'(2): clockwise 2 steps โ†’ press button (1 step). Total: 1 + 2 + 1 = 4 steps.
example_2.py โ€” Optimal Path Selection
$ Input: ring = "godding", key = "godding"
โ€บ Output: 13
๐Ÿ’ก Note: Spell each character optimally: g(0)โ†’1, o(1)โ†’2, d(2)โ†’1, d(3)โ†’2, i(4)โ†’2, n(5)โ†’2, g(6)โ†’2. Total: 1+2+1+2+2+2+2 = 12. Wait, let me recalculate: the optimal path gives 13 steps total.
example_3.py โ€” Single Character Ring
$ Input: ring = "a", key = "a"
โ€บ Output: 1
๐Ÿ’ก Note: The target character 'a' is already at position 0. Just press the button once. Total: 1 step.

Constraints

  • 1 โ‰ค ring.length, key.length โ‰ค 100
  • ring and key consist of only lowercase English letters
  • It's guaranteed that all characters in key exist in ring

Visualization

Tap to expand
godding12:00 PositionPRESSClockwise: 2 stepsCounter-clockwise: 4 stepsKey: "gd"Step 1: spell 'g' (1 step)Step 2: spell 'd' (3 steps)Total: 4 stepsRing: "godding"Choose optimal 'd' positionClockwise to pos 2: 2+1=3 steps
Understanding the Visualization
1
Initial State
Ring starts with first character at 12:00 position
2
Find Target
Locate all positions of the next character we need to spell
3
Calculate Distance
For each target position, calculate both clockwise and counterclockwise rotation distances
4
Choose Optimal
Select the position that minimizes total steps (rotation + button press + future moves)
5
Press Button
Press center button when target character is aligned at 12:00
Key Takeaway
๐ŸŽฏ Key Insight: Use Dynamic Programming to track the minimum steps to reach each ring position after spelling each character. For each target character, consider all its positions in the ring and choose the one requiring minimum total steps (rotation + remaining key spelling).
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~25 min Avg. Time
986 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