Skip to main content
Back to problems
#657
Easy Algorithms

Robot return to origin

String Simulation
76.5% acceptance
Feb 20, 2026
2587
753
Given a string moves of R, L, U, D, return true if the robot returns to the origin after all moves, false otherwise.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn judge_circle(moves: String) -> bool {
    let (mut x, mut y) = (0i32, 0i32);
    for c in moves.chars() {
      match c {
        'R' => x += 1,
        'L' => x -= 1,
        'U' => y += 1,
        'D' => y -= 1,
        _ => {}
      }
    }
    x == 0 && y == 0
  }
}