|
| 1 | +#!/usr/bin/python3 |
| 2 | +""" |
| 3 | +There is a robot starting at position (0, 0), the origin, on a 2D plane. Given |
| 4 | +a sequence of its moves, judge if this robot ends up at (0, 0) after it |
| 5 | +completes its moves. |
| 6 | +
|
| 7 | +The move sequence is represented by a string, and the character moves[i] |
| 8 | +represents its ith move. Valid moves are R (right), L (left), U (up), and D |
| 9 | +(down). If the robot returns to the origin after it finishes all of its moves, |
| 10 | +return true. Otherwise, return false. |
| 11 | +
|
| 12 | +Note: The way that the robot is "facing" is irrelevant. "R" will always make the |
| 13 | +robot move to the right once, "L" will always make it move left, etc. Also, |
| 14 | +assume that the magnitude of the robot's movement is the same for each move. |
| 15 | +
|
| 16 | +Example 1: |
| 17 | +
|
| 18 | +Input: "UD" |
| 19 | +Output: true |
| 20 | +Explanation: The robot moves up once, and then down once. All moves have the |
| 21 | +same magnitude, so it ended up at the origin where it started. Therefore, we |
| 22 | +return true. |
| 23 | +
|
| 24 | +
|
| 25 | +Example 2: |
| 26 | +
|
| 27 | +Input: "LL" |
| 28 | +Output: false |
| 29 | +Explanation: The robot moves left twice. It ends up two "moves" to the left of |
| 30 | +the origin. We return false because it is not at the origin at the end of its |
| 31 | +moves. |
| 32 | +""" |
| 33 | +from collections import Counter |
| 34 | + |
| 35 | + |
| 36 | +class Solution: |
| 37 | + def judgeCircle(self, moves: str) -> bool: |
| 38 | + counter = Counter(moves) |
| 39 | + return counter["L"] == counter["R"] and counter["U"] == counter["D"] |
0 commit comments