You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0.
In the ith move, you can choose one of the following directions:
moves[i] = 'L' or moves[i] = '_'moves[i] = 'R' or moves[i] = '_'Return the distance from the origin of the furthest point you can get to after n moves.
Example 1:
Input: moves = "L_RL__R" Output: 3 Explanation: The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves "LLRLLLR".
Example 2:
Input: moves = "_R__LL_" Output: 5 Explanation: The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves "LRLLLLL".
Example 3:
Input: moves = "_______" Output: 7 Explanation: The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves "RRRRRRR".
Constraints:
1 <= moves.length == n <= 50moves consists only of characters 'L', 'R' and '_'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a sequence of movement instructions where some directions are clear and others are blank, meaning they could be either left or right. The brute force approach tries every possible combination of replacing those blanks with actual directions and measures how far each combination ends up from the starting point.
Here's how the algorithm would work step-by-step:
def furthest_distance_from_origin(movement_instructions):
# We need to find all blank slots so we can generate every possible combination of directions
blank_positions = [index for index, direction in enumerate(movement_instructions) if direction == '_']
maximum_distance = 0
total_blanks = len(blank_positions)
# Testing every binary combination of left and right for the blanks ensures we explore all paths
for combination_index in range(1 << total_blanks):
current_instructions = list(movement_instructions)
for blank_index in range(total_blanks):
if (combination_index & (1 << blank_index)):
current_instructions[blank_positions[blank_index]] = 'L'
else:
current_instructions[blank_positions[blank_index]] = 'R'
current_position = 0
for movement in current_instructions:
if movement == 'L':
current_position -= 1
else:
current_position += 1
maximum_distance = max(maximum_distance, abs(current_position))
# Tracking the peak distance across all simulated paths guarantees we find the absolute maximum
return maximum_distanceTo find the furthest possible distance from the starting point after following a sequence of directions, the optimal strategy simply counts how many moves pull you to the left, how many pull you to the right, and how many are completely wildcards. Since the wildcard moves can be turned into whichever direction helps you travel the furthest, you can just boost your total count in whichever direction you are already leaning.
Here's how the algorithm would work step-by-step:
def furthest_distance_from_origin(movement_moves): # Count specific directional and wildcard moves to understand our current trajectory. left_move_count = movement_moves.count('L') right_move_count = movement_moves.count('R') wildcard_count = movement_moves.count('_') # Assign all wildcards to whichever direction we are already naturally leaning. if left_move_count >= right_move_count: left_move_count += wildcard_count else: right_move_count += wildcard_count # Calculate the net displacement from the origin by finding the absolute difference. return abs(left_move_count - right_move_count)| Case | How to Handle |
|---|---|
| String consists entirely of underscores | Counting the absolute difference between L and R is zero, so the number of underscores acts as the maximum possible distance. |
| String contains no underscores | The result simply evaluates the net absolute displacement of the fixed L and R moves. |
| Single character input like L, R, or underscore | The length of the string is one, and the logic correctly evaluates to a distance of one. |
| String with extremely large length up to the constraints limit | A single linear scan processes the string in O(N) time and O(1) space, easily scaling for large inputs. |
| Skewed distribution favoring only left or right moves along with underscores | Greedily turning all underscores into the dominant direction maximizes the absolute distance from the origin. |