#657
Robot Return to Origin
newbie · 240 · lc easy +18 · verified · 76.5% accepted · 2,588 likes · top 88%
Description
A robot starts at (0, 0) and follows a string of moves: 'R' (right), 'L' (left), 'U' (up), 'D' (down), each shifting position by one unit. Given the move string moves, return true if the robot ends back at the origin after all moves, or false otherwise.
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.
Code
1
2
3