forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPath Crossing.cpp
More file actions
39 lines (30 loc) · 746 Bytes
/
Path Crossing.cpp
File metadata and controls
39 lines (30 loc) · 746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Runtime: 3 ms (Top 74.64%) | Memory: 6.9 MB (Top 45.17%)
class Solution {
public:
bool isPathCrossing(string path) {
set<pair<int, int>>st;
int x=0,y=0;
st.insert({0, 0});
for(int i=0;i<path.length();i++){
if(path[i]=='N'){
x++;
}
else if(path[i]=='S'){
x--;
}
else if(path[i]=='E'){
y++;
}
else{
y--;
}
//if pair find at any point, return true
if(st.find({x,y}) != st.end()){
return 1;
}
//insert the pair
st.insert({x, y});
}
return 0;
}
};