1041. Robot Bounded In Circle - cocoder39/coco39_LC GitHub Wiki

1041. Robot Bounded In Circle

case 1: robot returns to origin after executing all instructions case 2: robot faces towards a direction different from the one he faced initially

class Solution:
    def isRobotBounded(self, instructions: str) -> bool:
        DIR = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        d = 0
        x, y = 0, 0
        for instruction in instructions:
            if instruction == 'L':
                d = (d - 1) % 4
            elif instruction == 'R':
                d = (d + 1) % 4
            else:
                dx, dy = DIR[d]
                x += dx
                y += dy
        
        return (x == 0 and y == 0) or d > 0