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 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]:
ring's characters at the "12:00" direction, where this character must equal key[i].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.
Example 1:
Input: ring = "godding", key = "gd" Output: 4 Explanation: For the first key character 'g', since it is already in place, we just need 1 step to spell this character. For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo". Also, we need 1 more step for spelling. So the final output is 4.
Example 2:
Input: ring = "godding", key = "godding" Output: 13
Constraints:
1 <= ring.length, key.length <= 100ring and key consist of only lower case English letters.key could always be spelled by rotating ring.In this approach, we'll use a recursive solution with memoization to efficiently calculate the minimum number of steps. The function will take the current position in the ring and the current index in the key string as parameters, and will calculate the optimal steps required for each character. We'll cache the results of subproblems to avoid recalculating them.
This solution defines a recursive function `dp` that returns the minimum steps from a given position `pos` in the ring to spell the remainder of the `key` starting from index `idx`. We use a hashmap `ring_indices` to find all positions in the `ring` that match the current character of `key`. For each potential position, we calculate the number of steps required to reach it from the current position, both clockwise and counter-clockwise, and add 1 for the press action. We store the solution in a memoization table to prevent recalculation.
Java
C++
Time Complexity: O(m*n), where m is the length of the `key` and n is the length of the `ring`.
Space Complexity: O(m*n) for the memoization table and index dictionary.
In this approach, we'll leverage BFS to explore each possible state of the ring and propagate the minimum distance for each configuration. We can view each state as a node in a graph and use BFS to find the target node with minimum steps.
This solution uses a breadth-first search (BFS) approach to explore each state (current position and current character index in the key string). Each state is added to a queue along with the action step count. For each character in the key, we consider all possible indices in the ring where the character appears and add them to the queue, marking them as visited.
Java
JavaScript
Time Complexity: O(n*m), where n is the length of the key and m is the length of the ring.
Space Complexity: O(n*m) for storing visited states in the queue.
| Approach | Complexity |
|---|---|
| Dynamic Programming with Memoization | Time Complexity: O(m*n), where m is the length of the `key` and n is the length of the `ring`. |
| Breadth-First Search Approach | Time Complexity: O(n*m), where n is the length of the key and m is the length of the ring. |
Freedom Trail Ring Combination Code - Fallout 4 • PunchNshoot • 1,022,796 views views
Watch 9 more video solutions →Practice Freedom Trail with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor