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:
Umeans move upDmeans move downLmeans move leftRmeans 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:
xfor horizontal positionyfor vertical position
Every move changes one of these coordinates:
UincreasesyDdecreasesyLdecreasesxRincreasesx
After processing all characters in the string:
- if
x == 0andy == 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:
- Initialize
x = 0andy = 0because the robot starts at the origin. - Loop through each character in the
movesstring. - Update the coordinates based on the direction.
- 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
Umoves equals the number ofDmoves - the number of
Lmoves equals the number ofRmoves
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
Uwith totalD - total
Lwith totalR
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
xandyto 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.