Leetcode 657. Robot Return to Origin
Problem Explanation
The problem is stating that we have a robot that can move in four directions, namely right (R), left (L), up (U) and down (D). These movements are provided as a string of letters. For example "ULD" means the robot moved up, left, and then down.
The question is asking us whether the robot ends up back at the starting position after performing all the moves. The starting position of the robot is (0,0) on the 2D plane. For the robot to return to the origin, it must have balanced its moves, meaning for every 'U' move, there should be a 'D' move and for every 'R' move, there should be an 'L' move. If the robot returned back to the origin after performing all the moves, we will return True otherwise False.
Letโs take an example. Suppose the move string is "UDLR". The robot moves Up, then Down, then Left and then Right. At the end, it returns to the origin point (0, 0). So, we return True. If the moves were "UUDL", the robot moves Up twice, then Down once and then Left once. It does not return to the origin, so we return False.
Solution Approach
To solve this task, we can count the moves for each direction, then we can check if for every 'U' move there is a 'D' move and for every 'R' move there is an 'L' move.
We can use a variable for up-down movements and another for right-left movements. Whenever we see a 'U' move, we increase the up-down variable by 1, and whenever we see a 'D' move, we decrease the up-down variable by 1. Similarly for 'R' and 'L' moves, we increase and decrease the right-left variable respectively.
At the end, if both variables are zero, the robot is at the origin, so we return True, otherwise we return False.
Python Solution:
1 2python 3class Solution: 4 def judgeCircle(self, moves: str) -> bool: 5 up = down = left = right = 0 6 for move in moves: 7 if move == 'U': 8 up += 1 9 elif move == 'D': 10 down += 1 11 elif move == 'R': 12 right += 1 13 elif move == 'L': 14 left += 1 15 return (up==down) and (left==right)
Java Solution:
1 2java 3public class Solution { 4 public boolean judgeCircle(String moves) { 5 int horizontal = 0; 6 int vertical = 0; 7 for (char move : moves.toCharArray()) { 8 if (move == 'U') vertical++; 9 if (move == 'D') vertical--; 10 if (move == 'R') horizontal++; 11 if (move == 'L') horizontal--; 12 } 13 return vertical == 0 && horizontal == 0; 14 } 15}
JavaScript Solution:
1 2javascript 3class Solution { 4 judgeCircle(moves) { 5 let vertical = 0; 6 let horizontal = 0; 7 for (let move of moves) { 8 switch (move) { 9 case 'U': 10 vertical++; 11 break; 12 case 'D': 13 vertical--; 14 break; 15 case 'R': 16 horizontal++; 17 break; 18 case 'L': 19 horizontal--; 20 break; 21 } 22 } 23 return vertical === 0 && horizontal === 0; 24 } 25}
C++ Solution:
1
2c++
3class Solution {
4public:
5 bool judgeCircle(string moves) {
6 int horizontal = 0;
7 int vertical = 0;
8 for (char move : moves) {
9 switch(move) {
10 case 'U':
11 vertical++;
12 break;
13 case 'D':
14 vertical--;
15 break;
16 case 'R':
17 horizontal++;
18 break;
19 case 'L':
20 horizontal--;
21 break;
22 }
23 }
24 return (horizontal == 0) && (vertical == 0);
25 }
26};
C# Solution:
1
2csharp
3public class Solution {
4 public bool JudgeCircle(string moves) {
5 int horizontal = 0, vertical = 0;
6 foreach (char move in moves) {
7 switch(move){
8 case 'U':
9 vertical++;
10 break;
11 case 'D':
12 vertical--;
13 break;
14 case 'R':
15 horizontal++;
16 break;
17 case 'L':
18 horizontal--;
19 break;
20 }
21 }
22 return horizontal == 0 && vertical == 0;
23 }
24}
Ruby Solution:
1 2ruby 3class Solution 4 def judge_circle(moves) 5 vertical = 0 6 horizontal = 0 7 moves.chars.each do |move| 8 case move 9 when 'U' 10 vertical += 1 11 when 'D' 12 vertical -= 1 13 when 'R' 14 horizontal += 1 15 when 'L' 16 horizontal -= 1 17 end 18 end 19 return vertical == 0 && horizontal == 0 20 end 21end
Swift Solution:
1
2swift
3class Solution {
4 func judgeCircle(_ moves: String) -> Bool {
5 var ver = 0
6 var hor = 0
7 for move in moves {
8 switch move {
9 case "U":
10 ver += 1
11 case "D":
12 ver -= 1
13 case "R":
14 hor += 1
15 case "L":
16 hor -= 1
17 default:
18 break
19 }
20 }
21 return ver == 0 && hor == 0
22 }
23}
Rust Solution:
1
2rust
3impl Solution {
4 pub fn judge_circle(moves: String) -> bool {
5 let (mut ver, mut hor) = (0, 0);
6 for ch in moves.chars() {
7 match ch {
8 'U' => ver += 1,
9 'D' => ver -= 1,
10 'R' => hor += 1,
11 'L' => hor -= 1,
12 _ => (),
13 }
14 }
15 hor == 0 && ver == 0
16 }
17}
Go Solution:
1
2go
3func judgeCircle(moves string) bool {
4 ver, hor := 0, 0
5 for _, move := range moves {
6 switch move {
7 case 'U':
8 ver += 1
9 case 'D':
10 ver -= 1
11 case 'R':
12 hor += 1
13 case 'L':
14 hor -= 1
15 }
16 }
17 return ver == 0 && hor == 0
18}
In each of these solutions, we iteratively check each move made by the robot in the provided string of movement commands. If the move is 'U', we increment the vertical counter (or the up-down counter) by 1. If the move is 'D', we decrement the vertical counter by 1. Similar actions are performed for 'R' and 'L' moves on the horizontal counter (or the right-left counter). At the end of all the moves, if both vertical and horizontal counters are 0, it signifies that the robot has returned back to the origin, and we return True. Otherwise, we return False.
Got a question?ย Ask the Teaching Assistantย anything you don't understand.
Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.