#2833

Furthest Point From Origin

pupil · 345 · lc easy +22 · verified · 65.3% accepted · 277 likes · top 70%

Description

A string moves of length n uses only 'L', 'R', and '_'. Starting at the origin on a number line:

- At step i, move left if moves[i] = 'L' or moves[i] = '_', or right if moves[i] = 'R' or moves[i] = '_'.

Return the greatest distance from the origin reachable after all 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".

Code

1
2
3