Robot Return to Origin (LeetCode 657): Solution and Explanation

Easy
String
Simulation

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

  1. Set x = 0 and y = 0.
  2. For each character: U adds one to y, D subtracts one from y, L subtracts one from x, R adds one to x.
  3. After the loop, return x == 0 and y == 0.

Step-by-step example

moves = "UD"
  1. Start at (0, 0).
  2. U moves the robot up, so y = 1 and the position is (0, 1).
  3. D moves it down again, so y = 0 and the position is (0, 0).
  4. 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 == 0
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each of the n moves is read once and does constant work.

Space

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 L and R mean. 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.