Description
A robot on a 2D plane starts at the origin (0, 0). Given a sequence of moves (U=up, D=down, L=left, R=right), determine if the robot returns to the origin after completing all moves.
Examples
Input:
moves = "UD"Output:
trueExplanation:
Up then down returns to origin.
Input:
moves = "LL"Output:
falseExplanation:
Two left moves end at position (-2, 0), not at the origin.
Input:
moves = "UDLR"Output:
trueExplanation:
The robot moves up (+1 y), down (-1 y), left (-1 x), then right (+1 x). All movements cancel out: net displacement is (0, 0), so it returns to origin.
Constraints
- •
1 ≤ moves.length ≤ 2 × 10⁴