#2833
Easy Algorithms Furthest point from origin
String Counting
65.3% acceptance
Feb 25, 2026
277
52
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' 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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn furthest_distance_from_origin(moves: String) -> i32 {
let bytes = moves.as_bytes();
let l = bytes.iter().filter(|&&c| c == b'L').count() as i32;
let r = bytes.iter().filter(|&&c| c == b'R').count() as i32;
let blanks = bytes.len() as i32 - l - r;
(l - r).abs() + blanks
}
}