LeetCode 657: Robot Return to Origin Python Solution

If you are solving LeetCode 657: Robot Return to Origin, this article gives you a clear and beginner-friendly explanation, along with an optimized Python solution, examples, and complexity analysis.

This is an easy LeetCode problem that tests your understanding of string traversal, simulation, and coordinate movement.

Problem Overview

A robot starts at the origin point (0, 0) on a 2D plane.

You are given a string called moves, where each character represents a direction:

  • U means move up
  • D means move down
  • L means move left
  • R means move right

Your task is to return true if the robot comes back to the origin after performing all the moves. Otherwise, return false.

Example

Example 1

Input:
moves = "UD"

Output:
true

Explanation:
The robot moves up once and down once, so it returns to (0, 0).

Example 2

Input:
moves = "LL"

Output:
false

Explanation:
The robot moves left twice and ends at (-2, 0).


How to Solve LeetCode 657

To solve Robot Return to Origin, we track the robotโ€™s current position using two variables:

  • x for horizontal position
  • y for vertical position

Every move changes one of these coordinates:

  • U increases y
  • D decreases y
  • L decreases x
  • R increases x

After processing all characters in the string:

  • if x == 0 and y == 0, the robot returned to the origin
  • otherwise, it did not

This is a straightforward simulation approach.


Python Solution for LeetCode 657

class Solution:
def judgeCircle(self, moves: str) -> bool:
x = 0
y = 0 for move in moves:
if move == 'U':
y += 1
elif move == 'D':
y -= 1
elif move == 'L':
x -= 1
elif move == 'R':
x += 1 return x == 0 and y == 0

Explanation of the Code

Letโ€™s break it down:

  1. Initialize x = 0 and y = 0 because the robot starts at the origin.
  2. Loop through each character in the moves string.
  3. Update the coordinates based on the direction.
  4. At the end, check whether both coordinates are back to zero.

If both are zero, the robot is back at the origin.


Why This Approach Works

The robot returns to the starting point only when:

  • the number of U moves equals the number of D moves
  • the number of L moves equals the number of R moves

Tracking the coordinates directly helps us verify this in a simple and intuitive way.

This makes the solution both efficient and easy to understand.


Complexity Analysis

Time Complexity

O(n)

We iterate through the moves string once, where n is the length of the string.

Space Complexity

O(1)

We only use two integer variables, so the extra space used is constant.


Alternative Python Solution

There is also a shorter way to solve this problem using count():

class Solution:
def judgeCircle(self, moves: str) -> bool:
return moves.count('U') == moves.count('D') and moves.count('L') == moves.count('R')

How it works

This solution compares:

  • total U with total D
  • total L with total R

If both pairs are equal, the robot returns to the origin.

Note

This version is shorter, but the simulation approach is usually better for interviews because it shows your thought process more clearly.


Interview Tip

In coding interviews, the best way to explain this problem is:

โ€œI simulate the robotโ€™s movement by tracking horizontal and vertical coordinates. After processing all moves, I check whether the final position is (0, 0).โ€

This explanation is simple, correct, and easy for interviewers to follow.


Common Mistakes

A few common mistakes while solving LeetCode 657:

  • forgetting to initialize both x and y to zero
  • mixing up horizontal and vertical movement
  • returning true too early before processing the full string
  • not checking both coordinates at the end

Final Thoughts

LeetCode 657: Robot Return to Origin is a great beginner-friendly problem for practicing movement simulation and string processing.

The key idea is simple:

  • update coordinates for each move
  • check whether the robot ends at (0, 0)

If you are preparing for coding interviews or improving your problem-solving skills, this is a good problem to master early.

Leave a Comment