You are given a string moves
that represents the movements of a robot on a 2D plane. The robot starts at the origin point (0, 0), and each character in the string moves it as follows:
Return True
if the robot returns to the origin after completing all of its moves, or False
otherwise.
moves
will be between 1 and 10^4.x
and y
to track the robot’s position, starting both at 0.x
and y
coordinates according to the move.True
; otherwise, return False
.def judgeCircle(moves: str) -> bool:
x, y = 0, 0
for move in moves:
if move == 'U':
y += 1
elif move == 'D':
y -= 1
elif move == 'L':
x -= 1
elif move == 'R':
x += 1
return x == 0 and y == 0
n
is the length of the input string moves
. This is because we iterate over the string once, and each operation inside the loop is constant time.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?