LeetCode 1496: Path Crossing [EASY] zhijie, February 5, 2024June 10, 2024 Path Crossing – LeetCode Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path. Return true if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false otherwise. class Solution: def isPathCrossing(self, path: str) -> bool: x, y = 0,0 visited = set() visited.add((x,y)) for c in path: if 'n' == c.lower(): y += 1 elif 's' == c.lower(): y -= 1 elif 'e' == c.lower(): x -= 1 else: x += 1 print(visited) if ((x,y) in visited): return True visited.add((x,y)) return False Share this:FacebookXLike this:Like Loading... Related LeetCode LeetCode