Freedom Trail - Problem
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
- You need to spell each character of a
- Initially,
- For each character in
โข 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
- '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
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 -
ringandkeyconsist of only lowercase English letters -
It's guaranteed that all characters in
keyexist inring
Visualization
Tap to expand
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).
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code