Robot Return to Origin

A robot starts at (0, 0) on a 2D plane. Given a string `moves` of its moves, where each move is "R" (right), "L" (left), "U" (up) or "D" (down), return true if the robot ends back at (0, 0) after all the moves, and false otherwise. Each move has the same length and the robot's facing does not matter.

30:00

Robot Return to Origin
easy
Free written solution
Topics
Companies

A robot starts at (0, 0) on a 2D plane. Given a string moves of its moves, where each move is "R" (right), "L" (left), "U" (up) or "D" (down), return true if the robot ends back at (0, 0) after all the moves, and false otherwise. Each move has the same length and the robot's facing does not matter.

Example 1:
Input: {"moves":"UD"}
Output: true
Constraints:
  • 1 <= moves.length <= 2 * 10^4

  • moves only contains the characters 'U', 'D', 'L' and 'R'.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"moves":"UD"}

The robot stands on the origin (0, 0). Every U must be cancelled by a D and every L by an R for it to come home, so track x and y while replaying the 2 moves.

moves

applied 0/2

U
D

the plane

robot at (0, 0) = origin

dashed green ring = origin (0, 0) · dot = robot

y = +0

↑ U

0

↓ D

0

x = +0

→ R

0

← L

0

Variables
VariableValue
x0
y0
DepthFunction Call
Stack empty
0/4