Leetcode 1619 Solution

This article provides solution to leetcode question 1619 (path-crossing)

https://leetcode.com/problems/path-crossing

Solution

class Solution:
    def isPathCrossing(self, path: str) -> bool:
        loc = (0, 0)
        visited = {loc}

        for ch in path:
            if ch == 'N':
                loc = (loc[0] - 1, loc[1])
            elif ch == 'E':
                loc = (loc[0], loc[1] + 1)
            elif ch == 'W':
                loc = (loc[0], loc[1] - 1)
            elif ch == 'S':
                loc = (loc[0] + 1, loc[1])

            if loc in visited:
                return True

            visited.add(loc)

        return False