
Problem Statement
In this problem, a robot initiates its movement from the origin, coordinate (0,0)
, on a two-dimensional plane. Throughout its journey, it follows a sequence of steps predefined in the string moves
, where each character indicates a specific direction ('L'
for left, 'R'
for right, 'U'
for up, and 'D'
for down). The challenge is to determine if, after executing all the moves in the sequence, the robot returns exactly to its starting point at the origin. The function should return true
if the robot does return to (0, 0)
, and false
otherwise. Each move is of uniform magnitude, and the orientation or 'facing direction' of the robot is not necessary for the computation of its final position.
Examples
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 and Intuition
When considering how to determine if the robot returns to the origin, it becomes apparent that each move can either cancel out another move or contribute to a net displacement from the origin:
Each 'R' (right) and 'L' (left) move impacts the robot's x-coordinate.
- 'R' moves the robot one unit to the right, and 'L' one unit to the left.
- If the robot makes equal numbers of 'R' and 'L' moves, their effects cancel out, resulting in a net x-coordinate of 0.
Each 'U' (up) and 'D' (down) move impacts the robot's y-coordinate.
- 'U' moves the robot one unit upward, and 'D' one unit downward.
- Similarly, if the robot makes equal numbers of 'U' and 'D' moves, their effects cancel out, resulting in a net y-coordinate of 0.
To determine if the robot returns to the origin:
- Count the occurrences of each move type.
- Check if the counts of 'L' matches 'R' and if the counts of 'U' matches 'D'.
- If both conditions hold true, the robot returns to the origin, and the function should return
true
. If either condition fails, the function should returnfalse
.
This logic effectively checks for a balance in horizontal and vertical movements, ensuring that any movement away from the origin in any direction is matched by an equivalent movement back towards the origin.
Solutions
- Java
class Solution {
public boolean checkRobotReturnToOrigin(String path) {
int horizontal = 0, vertical = 0;
for (char direction : path.toCharArray()) {
switch (direction) {
case 'U': vertical--; break;
case 'D': vertical++; break;
case 'L': horizontal--; break;
case 'R': horizontal++; break;
}
}
return horizontal == 0 && vertical == 0;
}
}
The Java solution provided determines if a robot, after moving through a series of commands, returns to its original starting position. The commands for movement are represented by the string "path", where each character ('U', 'D', 'L', 'R') corresponds to a direction (Up, Down, Left, Right).
- Initialize two counters,
horizontal
andvertical
, to track the robot's position along the x-axis and y-axis. - Convert the string
path
into a character array and iterate over it using a for-each loop. - Use a switch statement to adjust the
horizontal
orvertical
counters based on the current direction:- 'U' decreases the
vertical
count (move up). - 'D' increases the
vertical
count (move down). - 'L' decreases the
horizontal
count (move left). - 'R' increases the
horizontal
count (move right).
- 'U' decreases the
- After processing all commands, check if both
horizontal
andvertical
are zero. If true, the robot has returned to the origin.
Return true
if the robot returns to the origin; otherwise, return false
. This solution operates in O(n) time complexity, where n is the length of the path, as it requires a single scan of the input string to evaluate the robot's final position.
No comments yet.