Furthest Point From Origin
EasyUpdated: Aug 2, 2025
Practice on:
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 _thedistance from the origin of the furthest point you can get to after _n moves.
Examples
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 <= 50movesconsists only of characters'L','R'and'_'.
Solution
Method 1 – Counting L, R, and Underscores
Intuition
Each 'L' moves left, each 'R' moves right, and each '' can be used as either. To get the furthest distance, use all '' in one direction (either all left or all right), and add to the count of L or R, whichever is larger.
Approach
- Count the number of 'L', 'R', and '_' in the string.
- The furthest distance is
abs(count_L - count_R) + count_underscore. - Return the result.
Code
C++
class Solution {
public:
int furthestDistanceFromOrigin(string moves) {
int l = 0, r = 0, u = 0;
for (char c : moves) {
if (c == 'L') l++;
else if (c == 'R') r++;
else u++;
}
return abs(l - r) + u;
}
};
Go
type Solution struct{}
func (Solution) furthestDistanceFromOrigin(moves string) int {
l, r, u := 0, 0, 0
for _, c := range moves {
if c == 'L' {
l++
} else if c == 'R' {
r++
} else {
u++
}
}
if l > r {
return l - r + u
}
return r - l + u
}
Java
class Solution {
public int furthestDistanceFromOrigin(String moves) {
int l = 0, r = 0, u = 0;
for (char c : moves.toCharArray()) {
if (c == 'L') l++;
else if (c == 'R') r++;
else u++;
}
return Math.abs(l - r) + u;
}
}
Kotlin
class Solution {
fun furthestDistanceFromOrigin(moves: String): Int {
var l = 0; var r = 0; var u = 0
for (c in moves) {
when (c) {
'L' -> l++
'R' -> r++
else -> u++
}
}
return kotlin.math.abs(l - r) + u
}
}
Python
class Solution:
def furthestDistanceFromOrigin(self, moves: str) -> int:
l = moves.count('L')
r = moves.count('R')
u = moves.count('_')
return abs(l - r) + u
Rust
impl Solution {
pub fn furthest_distance_from_origin(moves: String) -> i32 {
let l = moves.chars().filter(|&c| c == 'L').count() as i32;
let r = moves.chars().filter(|&c| c == 'R').count() as i32;
let u = moves.chars().filter(|&c| c == '_').count() as i32;
(l - r).abs() + u
}
}
TypeScript
class Solution {
furthestDistanceFromOrigin(moves: string): number {
let l = 0, r = 0, u = 0;
for (const c of moves) {
if (c === 'L') l++;
else if (c === 'R') r++;
else u++;
}
return Math.abs(l - r) + u;
}
}
Complexity
- ⏰ Time complexity:
O(n), wherenis the length of the string, since each character is processed once. - 🧺 Space complexity:
O(1), as only counters are used.