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: # Time: O(n) # Space: O(n) def is_path_crossing(self, path: str) -> bool: x = y = 0 seen = {(0, 0)} moves = {"N": (0, 1), "S": (0, -1), "E": (1, 0), "W": (-1, 0)} for step in path: dx, dy = moves[step] x, y = x + dx, y + dy if (x, y) in seen: return True seen.add((x, y)) return False