-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsPathCrossing.java
More file actions
29 lines (25 loc) · 823 Bytes
/
Copy pathIsPathCrossing.java
File metadata and controls
29 lines (25 loc) · 823 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
package LeetcodePractice;
import java.util.HashSet;
import java.util.Set;
public class IsPathCrossing {
public boolean isPathCrossing(String path) {
Set<String> visited = new HashSet<>();
int x = 0, y = 0;
visited.add(x + "," + y);
for (char c : path.toCharArray()) {
if (c == 'N') y++;
else if (c == 'S') y--;
else if (c == 'E') x++;
else if (c == 'W') x--;
String pos = x + "," + y;
if (visited.contains(pos)) return true;
visited.add(pos);
}
return false;
}
public static void main(String[] args) {
IsPathCrossing sol = new IsPathCrossing();
System.out.println(sol.isPathCrossing("NES"));
System.out.println(sol.isPathCrossing("NESWW"));
}
}