class Solution:
def isRobotBounded(self, instructions: str) -> bool:
# time O(N) space O(1)
#step1 initialize clockwise direction momentum changes, start point, momentum pointer
dirs = [(0,1),[1,0],(0,-1),(-1,0)]
x,y = 0,0
i = 0
#face north, so dx,dy = 0, 1 => i = 1
# step2: iterate instructions
for ins in instructions:
dx = dirs[i][0]
dy = dirs[i][1]
# step2.1: each time only G is executed, x,y postion can be updated, otherwise, pointer will update based on 'L'/'G'
if ins == 'G':
x = x + dx
y = y + dy
elif ins == 'L':
i = (i + 3)%4 # use module to limit pointer range from [0,3]
elif ins == 'R':
i = (i+1)%4
# case 1: after executing instructions robot returns to origin
if x == 0 and y == 0 :
return True
# case 2: after executing instructions robot dirrection not facing at north
return (dirs[i][0],dirs[i][1]) != (0,1)