Leetcode problem: 657. Robot Return to Origin

Description:

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'.

Solution:

public class Solution {
public bool JudgeCircle(string moves) {
var arrDestination = new int[] {0,0};

var len = moves.Length;

for(var i=0;i<len;i++){
if(moves[i] == 'U'){
arrDestination[1] = arrDestination[1]+1;
}
else if(moves[i] == 'D'){
arrDestination[1] = arrDestination[1]-1;
}
else if(moves[i] == 'R'){
arrDestination[0] = arrDestination[0]+1;
}
else if(moves[i] == 'L'){
arrDestination[0] = arrDestination[0]-1;
}
}

if(arrDestination[0] == 0 && arrDestination[1] == 0) return true;
return false;
}
}

Explanation:

public class Solution {
public bool JudgeCircle(string moves) {

This defines a class Solution and a method JudgeCircle.

The method takes a string moves, which represents a sequence of moves the robot will make.

‘U’ = Up

‘D’ = Down

‘L’ = Left

‘R’ = Right

The method returns a boolean (true or false) depending on whether the robot ends at the starting position.
var arrDestination = new int[] {0,0};
arrDestination is an array of two integers that keeps track of the robot’s current position.

arrDestination[0] → x-coordinate (horizontal, left/right)

arrDestination[1] → y-coordinate (vertical, up/down)

Initially, the robot is at (0,0).
var len = moves.Length;
len stores the number of moves in the input string.


for(var i=0;i<len;i++){
This is a loop that goes through each move in the string moves.
if(moves[i] == ‘U’){
arrDestination[1] = arrDestination[1]+1;
}

If the move is ‘U’ (up), increase the y-coordinate by 1.
else if(moves[i] == ‘D’){
arrDestination[1] = arrDestination[1]-1;
}

If the move is ‘D’ (down), decrease the y-coordinate by 1.
else if(moves[i] == ‘R’){
arrDestination[0] = arrDestination[0]+1;
}

If the move is ‘R’ (right), increase the x-coordinate by 1.
else if(moves[i] == ‘L’){
arrDestination[0] = arrDestination[0]-1;
}

If the move is ‘L’ (left), decrease the x-coordinate by 1.

After the loop finishes, arrDestination holds the robot’s final position after all moves.
if(arrDestination[0] == 0 && arrDestination[1] == 0) return true;
return false;
}
}

This checks if the robot is back at (0,0).

If both x and y are 0, return true → the robot returned to the start.

Otherwise, return false.

Example
moves = “UDLR”
Step-by-step positions:

Start: (0,0)

U → (0,1)

D → (0,0)

L → (-1,0)

R → (0,0)

Final position: (0,0) → returns true.

✅ In short:
This code tracks the robot’s movement on a grid and checks if, after following all moves, it ends up at its starting point.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , Smart Fitness Guide , CodesToolbox , Testing Forum
Scroll to Top
0
Would love your thoughts, please comment.x
()
x