← Back to DSA
#657

Robot Return to Origin

Easy
StringSimulation
Solve on LeetCode ↗

Robot Return to Origin

Problem

We are given a string moves where:

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

The robot starts at (0, 0).

We need to return true if, after performing all moves, the robot is back at the origin. Otherwise return false.

Intuition

We do not need to remember the full path.

The only thing that matters is the robot's final position.

So we can simulate each move, update x and y, and check whether both coordinates become 0 again at the end.

Approach #1: Simulation

Initially, the robot is at (x, y) = (0, 0).

For every character:

  • if the move is U, increase y by 1
  • if the move is D, decrease y by 1
  • if the move is L, decrease x by 1
  • if the move is R, increase x by 1

Some explanations flip the sign of the y axis. That is also fine as long as U and D are handled consistently. The key idea is still the same: simulate the robot's movement and inspect the final coordinates.

Algorithm

  1. Initialize x = 0 and y = 0
  2. Traverse the string moves
  3. Update the coordinates based on the current move
  4. After the loop, return x == 0 && y == 0

This is the most direct solution and fits the problem perfectly.

Code Solution

Switch between languages

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0;
        int y = 0;

        for (int i = 0; i < moves.length(); i++) {
            char move = moves.charAt(i);

            switch (move) {
                case 'U':
                    y++;
                    break;
                case 'D':
                    y--;
                    break;
                case 'L':
                    x--;
                    break;
                case 'R':
                    x++;
                    break;
                default:
                    break;
            }
        }

        return x == 0 && y == 0;
    }
}

Dry Run

Take moves = "UDLR".

  • Start at (0, 0)
  • U -> (0, 1)
  • D -> (0, 0)
  • L -> (-1, 0)
  • R -> (0, 0)

The robot ends at the origin, so the answer is true.

Now take moves = "UUDL".

  • Start at (0, 0)
  • U -> (0, 1)
  • U -> (0, 2)
  • D -> (0, 1)
  • L -> (-1, 1)

The final position is not (0, 0), so the answer is false.

Why This Works

Every move changes the position by exactly one unit in one direction.

So after processing the whole string, (x, y) represents the robot's exact final location.

If the final location is (0, 0), the robot returned to the origin. If it is anything else, it did not.

That is exactly what the problem asks us to check.

Complexity Analysis

Time Complexity: O(n), where n is the length of moves

We process each character once.

Space Complexity: O(1)

We only use two integer variables, x and y.

If a Java solution first converts the string into a separate character array, that version would use O(n) extra space. The implementations above avoid that.

Common Mistakes

1. Tracking more than needed

We do not need to store every position in the path.

Only the final coordinates matter.

2. Mixing up directions

Be careful with:

  • L -> x - 1
  • R -> x + 1
  • U and D changing y

3. Checking only one coordinate

Returning to the origin means:

  • x == 0
  • y == 0

Both conditions must be true.

Final Takeaway

This is a classic simulation problem.

Whenever a problem asks you to follow instructions step by step, one clean strategy is:

  • keep the current state
  • update it for each command
  • check the final state

That is exactly why this solution is simple, fast, and accepted.