Furthest Point From Origin

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 i^th move, you can choose one of the following directions:

  • Move to the left if moves[i] = 'L' or moves[i] = '_'.
  • Move to the right if 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
Inputmoves = "L_RL__R"
Output3
The furthest point we can reach from the origin 0 is point -3 through the sequence of moves "LLRLLLR".
Example 2
Inputmoves = "_R__LL_"
Output5
The furthest point we can reach from the origin 0 is point -5 through the sequence of moves "LRLLLLL".

Constraints

  • 1 <= moves.length == n <= 50
  • moves consists only of characters 'L', 'R' and '_'.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate