Furthest Point From Origin - Problem
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:
- move to the left if
moves[i] = 'L'ormoves[i] = '_' - move to the right if
moves[i] = 'R'ormoves[i] = '_'
Return the distance from the origin of the furthest point you can get to after n moves.
Input & Output
Example 1 — Basic Case
$
Input:
moves = "L_RL__R"
›
Output:
3
💡 Note:
We have L=2, R=2, _=3. Net from fixed moves: 2-2=0. Since net is 0, we can add all flexible moves in either direction to get maximum distance: 0+3=3.
Example 2 — All Flexible
$
Input:
moves = "___"
›
Output:
3
💡 Note:
All moves are flexible. Assign all to same direction (e.g., all R) gives distance 3.
Example 3 — No Flexible Moves
$
Input:
moves = "LLR"
›
Output:
1
💡 Note:
No flexible moves. Final position: -1-1+1=-1, distance is 1.
Constraints
- 1 ≤ moves.length ≤ 2000
- moves[i] is either 'L', 'R', or '_'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code