Robot Return to Origin (LeetCode 657): Solution and Explanation
A robot starts at (0, 0) and follows a string of moves made of U, D, L and R. Decide whether it ends back on the origin.
Intuition
Each move shifts exactly one coordinate by one unit: U and D change y, L and R change x. The final position is just the sum of all those shifts.
Addition does not care about order, so the path the robot takes is irrelevant. Only the totals matter, and the robot is home exactly when the number of U equals the number of D and the number of L equals the number of R.
Keeping two integers while scanning the string captures both conditions at once, with no extra memory.
Approach: Simulate the position
- Set
x = 0andy = 0. - For each character:
Uadds one toy,Dsubtracts one fromy,Lsubtracts one fromx,Radds one tox. - After the loop, return
x == 0 and y == 0.
Step-by-step example
moves = "UD"
- Start at
(0, 0). Umoves the robot up, soy = 1and the position is(0, 1).Dmoves it down again, soy = 0and the position is(0, 0).- Both coordinates are 0, so the answer is
true.
Code: Python, Java, C++
1class Solution:
2 def judgeCircle(self, moves: str) -> bool:
3 x = y = 0
4 for m in moves:
5 if m == 'U':
6 y += 1
7 elif m == 'D':
8 y -= 1
9 elif m == 'L':
10 x -= 1
11 else:
12 x += 1
13 return x == 0 and y == 01class Solution {
2 public boolean judgeCircle(String moves) {
3 int x = 0, y = 0;
4 for (char m : moves.toCharArray()) {
5 if (m == 'U') {
6 y++;
7 } else if (m == 'D') {
8 y--;
9 } else if (m == 'L') {
10 x--;
11 } else {
12 x++;
13 }
14 }
15 return x == 0 && y == 0;
16 }
17}1class Solution {
2public:
3 bool judgeCircle(string moves) {
4 int x = 0, y = 0;
5 for (char m : moves) {
6 if (m == 'U') {
7 y++;
8 } else if (m == 'D') {
9 y--;
10 } else if (m == 'L') {
11 x--;
12 } else {
13 x++;
14 }
15 }
16 return x == 0 && y == 0;
17 }
18};Time and space complexity
O(n)
Each of the n moves is read once and does constant work.
O(1)
Only the two coordinates are stored, whatever the length of the input.
Other approaches
Count characters (time O(n), space O(1))
Count each letter (for example with moves.count('U') or a frequency map) and check U == D and L == R. It is the same idea expressed as counting, and some languages make it a one-liner, though it may scan the string several times.
Record the whole path (time O(n), space O(n))
Store every visited point in a list and look at the last one. It works but uses O(n) memory for information the problem never asks for.
Common mistakes
- Checking only one axis, for example
U == D, and forgetting that the robot can drift left or right. - Returning true as soon as the robot passes through the origin mid-way. Only the final position counts.
- Thinking the robot's facing changes what
LandRmean. The moves are absolute directions on the plane.
Frequently asked questions
Does the order of the moves matter?
No. The final position is the sum of the individual shifts, and a sum is the same in any order, so UDLR and LRUD end in the same place.
Can I stop early?
Not safely. Even if the robot is far away halfway through, later moves can bring it back, so you need to process every move.
Can the coordinates overflow?
No. There are at most 20,000 moves, so each coordinate stays between -20,000 and 20,000, far inside a 32-bit integer.
What is the time and space complexity?
O(n) time for one pass and O(1) extra space for the two coordinates.