Tap here to switch tabs
Problem
Ask
Submissions

Problem: Freedom Trail

med
30 min
Explore the Freedom Trail coding challenge where you apply dynamic programming to minimize the steps needed to spell a keyword by rotating and pressing characters on a circular ring. This lesson teaches you how to optimize rotations and button presses to efficiently solve the problem while building your skills in advanced dynamic programming techniques.

Statement

You are given a circular ring represented by a string ring, and a keyword represented by a string key that you need to spell out.

Initially, the first character of ring (index 00) is aligned at the "12:00" position. To spell each character in key sequentially, you must:

  1. Rotate the ring clockwise or anticlockwise. Each rotation by one position counts as one step. Rotate until a character in ring that matches the current target character key[i] is aligned at the "12:00" position.

  2. Press the center button to confirm the character, which counts as one additional step.

After pressing, you move on to spell the next character in key. The ring is circular, meaning its last character wraps around to its first character.

Return the minimum total number of steps required to spell all characters in key.

Note: Each button press counts as one step in addition to the rotation steps.

Constraints:

  • 11 \leq ring.length, key.length 100\leq 100

  • ring and key consist of only lowercase English letters.

  • It is guaranteed that key can always be spelled by rotating ring.

canvasAnimation-image
1 / 3
Tap here to switch tabs
Problem
Ask
Submissions

Problem: Freedom Trail

med
30 min
Explore the Freedom Trail coding challenge where you apply dynamic programming to minimize the steps needed to spell a keyword by rotating and pressing characters on a circular ring. This lesson teaches you how to optimize rotations and button presses to efficiently solve the problem while building your skills in advanced dynamic programming techniques.

Statement

You are given a circular ring represented by a string ring, and a keyword represented by a string key that you need to spell out.

Initially, the first character of ring (index 00) is aligned at the "12:00" position. To spell each character in key sequentially, you must:

  1. Rotate the ring clockwise or anticlockwise. Each rotation by one position counts as one step. Rotate until a character in ring that matches the current target character key[i] is aligned at the "12:00" position.

  2. Press the center button to confirm the character, which counts as one additional step.

After pressing, you move on to spell the next character in key. The ring is circular, meaning its last character wraps around to its first character.

Return the minimum total number of steps required to spell all characters in key.

Note: Each button press counts as one step in addition to the rotation steps.

Constraints:

  • 11 \leq ring.length, key.length 100\leq 100

  • ring and key consist of only lowercase English letters.

  • It is guaranteed that key can always be spelled by rotating ring.

canvasAnimation-image
1 / 3