Skip to content

Commit 35c7b9e

Browse files
authored
Create path-crossing.py
1 parent 66ecbe2 commit 35c7b9e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Python/path-crossing.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def isPathCrossing(self, path):
6+
"""
7+
:type path: str
8+
:rtype: bool
9+
"""
10+
x = y = 0
11+
lookup = {(0, 0)}
12+
for c in path:
13+
if c == 'E':
14+
x += 1
15+
elif c == 'W':
16+
x -= 1
17+
elif c == 'N':
18+
y += 1
19+
elif c == 'S':
20+
y -= 1
21+
if (x, y) in lookup:
22+
return True
23+
lookup.add((x, y))
24+
return False

0 commit comments

Comments
 (0)