Watch 10 video solutions for Robot Return to Origin, a easy level problem involving String, Simulation. This walkthrough by Matt Upham | Tech + Coding has 1,758 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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. |