requirements

In two dimensions, I have a robot that starts at the origin (0, 0). Give the order of its movement and determine whether the robot ends at (0, 0) after it completes its movement.

The movement order is represented by a string. The character move[I] indicates its i-th move. The effective actions of the robot are R (right), L (left), U (up) and D (down). Returns true if the robot returns to the origin after completing all actions. Otherwise, return false.

Note: It doesn’t matter which way the robot is “facing”. “R” will always make the robot move to the right once, “L” will always move to the left, etc. Furthermore, assume that the robot moves by the same amount each time it moves.

Example 1:

Input: "UD" Output: true Explanation: The robot moves up once, then down once. All actions have the same magnitude, so it eventually returns to where it started. Therefore, we return true.Copy the code

Example 2:

Input: "LL" Output: false Explanation: Robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it does not return to the origin at the end of the move.Copy the code

The core code

class Solution:
    def judgeCircle(self, moves: str) - >bool:
        return moves.count("U") == moves.count("D")  and moves.count("L") == moves.count("R")
Copy the code

If the robot takes the same number of steps left and right, and the same number of steps up and down, it will be able to walk back to the origin.