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 * 104moves only contains the characters 'U', 'D', 'L' and 'R'.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.
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.
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.
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.
Time Complexity: O(n), where n is the length of the input moves.
Space Complexity: O(1), for direct counting with integer variables.
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:
'U', then y increases by 1;, then y decreases by 1;x decreases by 1;, 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)$.
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Balancing Moves | Time Complexity: O(n), where n is the length of the moves string. |
| Net Balance Using Counts | Time Complexity: O(n), where n is the length of the input |
| Maintain Coordinates | — |
| Approach | Time | Space | When 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 Counts | O(n) | O(1) | Useful when opposite operations cancel out and only final displacement matters. |
Amazon Coding Interview: Robot Return to Origin (LeetCode) • Matt Upham | Tech + Coding • 1,758 views views
Watch 9 more video solutions →Practice Robot Return to Origin with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor