Skip to main content

Robot Return to Origin - Solution & Explanation

EasyStringSimulation16 min readAsked at: Amazon, Microsoft, Goldman Sachs +2
Practice this problem

Problem Statement

There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).

Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.

Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

 

Example 1:

Input: moves = "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.

Example 2:

Input: moves = "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.

 

Constraints:

  • 1 <= moves.length <= 2 * 104
  • moves only contains the characters 'U', 'D', 'L' and 'R'.

Approach Overview

Problem Overview: You receive a string of moves where each character represents a step taken by a robot on a 2D grid: U, D, L, and R. The robot starts at coordinate (0,0). The task is to determine whether the sequence of moves brings the robot back to the origin after executing all steps.

Approach 1: Balancing Moves (O(n) time, O(1) space)

This approach simulates the robot’s movement directly. Initialize two integers x and y to represent the current coordinates. Iterate through the move string once. For each character, update the coordinates: U increments y, D decrements y, R increments x, and L decrements x. After processing all characters, check whether both coordinates returned to zero.

The key insight is that every movement changes exactly one axis by one unit. Tracking coordinates directly mirrors how the robot moves in a real grid. This solution works well for problems involving step‑by‑step movement simulation and is a common pattern in simulation problems. The algorithm performs a single pass over the input string, so the time complexity is O(n), and it uses only two integer variables, resulting in O(1) space.

Approach 2: Net Balance Using Counts (O(n) time, O(1) space)

This approach relies on the observation that opposite moves cancel each other. A robot returns to the origin only if the number of U moves equals the number of D moves and the number of L moves equals the number of R moves. Instead of updating coordinates step by step, iterate through the string and maintain four counters or use built‑in character counts.

After counting, compare the totals: count('U') == count('D') and count('L') == count('R'). If both conditions hold, the robot ends at the origin. This approach focuses on the net displacement rather than the path taken. Since it processes the string once and stores only a few counters, the complexity remains O(n) time and O(1) space.

This counting technique is common in string processing tasks where the relative frequency of characters determines the result. It removes coordinate tracking and makes the logic concise.

Recommended for interviews: Interviewers typically expect the coordinate simulation approach because it clearly models the robot’s movement and demonstrates understanding of grid traversal. The counting approach is equally optimal and slightly shorter, but explaining the cancellation idea shows deeper reasoning about the problem. Both solutions run in O(n) time with constant space, which is optimal for this problem.

Approach 1: Balancing Moves

This approach involves counting the number of moves in each direction and checking if the horizontal movements ('L' and 'R') balance each other and the vertical movements ('U' and 'D') balance each other to return to the origin.

We initialize two integer variables, x and y, to track the robot's position. We iterate through the moves string, adjusting the position according to each move. If 'U' is found, y is incremented; if 'D' is found, y is decremented; 'L' decrements x; and 'R' increments x. Finally, we check if both x and y are zero, indicating a return to the origin, and return accordingly.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the moves string.
Space Complexity: O(1), constant space used for variables x and y.

Try this approach in the editor →

Approach 2: Net Balance Using Counts

Instead of tracking coordinates, this approach counts the occurrences of each move character to determine if the robot returns to the origin. The idea is that the number of 'U' moves should equal 'D' moves and 'L' moves should equal 'R'.

We use four counters to track the number of each move type in the input string. The robot returns to the origin if 'U' matches 'D' and 'L' matches 'R'. Thus, we count the occurrences of each direction and compare. The function returns true if these conditions are met.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the input moves.
Space Complexity: O(1), for direct counting with integer variables.

Try this approach in the editor →

Approach 3: Maintain Coordinates

We can maintain a coordinate (x, y) to represent the robot's movement in the horizontal and vertical directions.

Traverse the string moves and update the coordinate (x, y) based on the current character:

  • If the current character is 'U', then y increases by 1;
  • If the current character is `'D', then y decreases by 1;
  • If the current character is `'L', then x decreases by 1;
  • If the current character is `'R', then x increases by 1.

Finally, check if both x and y are 0.

The time complexity is O(n), where n is the length of the string moves. The space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Balancing Moves

Time Complexity: O(n), where n is the length of the moves string.
Space Complexity: O(1), constant space used for variables x and y.

Net Balance Using Counts

Time Complexity: O(n), where n is the length of the input moves.
Space Complexity: O(1), for direct counting with integer variables.

Maintain Coordinates

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Balancing Moves (Coordinate Simulation)O(n)O(1)Best for interview explanations and grid movement problems where you simulate position changes.
Net Balance Using CountsO(n)O(1)Useful when opposite operations cancel out and only final displacement matters.

Video Solution

Robot Return to Origin | Simple Explanation | Leetcode 657 | codestorywithMIKcodestorywithMIK3,283 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Robot Return to Origin easy or hard?
Robot Return to Origin is classified as an Easy problem on LeetCode with a high acceptance rate. The challenge mainly tests basic string traversal and simple simulation logic rather than advanced algorithms.
Robot Return to Origin Python/Java solution
In Python or Java, iterate through the string and update coordinates for each move. For example, increment y for 'U', decrement y for 'D', increment x for 'R', and decrement x for 'L'. After the loop, return true if both coordinates equal zero. The implementation runs in O(n) time and O(1) space.
How to solve Robot Return to Origin in O(n)?
Iterate through the move string once and track the robot’s position using two integers for x and y. Increment or decrement the coordinates based on the move character. After processing all moves, return true if x == 0 and y == 0; otherwise return false.
What is the best approach for Robot Return to Origin?
The best approach is simulating the robot's coordinates while iterating through the move string. Update x and y positions for each move (U, D, L, R) and check if both return to zero. This solution runs in O(n) time with O(1) space and clearly models the robot’s movement on a grid.
Is Robot Return to Origin asked at Google/Amazon/Meta?
Robot Return to Origin is a common easy-level interview question used by companies like Amazon and Google to test basic string processing and simulation skills. It checks whether candidates can model movement logic and reason about net displacement efficiently.
What data structure is used in Robot Return to Origin?
The problem typically uses simple variables or counters rather than complex data structures. Most solutions rely on integer variables for coordinates or character frequency counts while iterating through the string.
What is the time complexity of Robot Return to Origin?
The optimal time complexity is O(n), where n is the length of the move string. The algorithm scans the string once and performs constant-time updates for each character. Space complexity is O(1) since only a few counters or coordinate variables are required.

Ready to solve this problem?

Practice Robot Return to Origin with our built-in code editor and test cases.

Practice on FleetCode