Skip to content

Commit

Permalink
Sync LeetCode submission - Path Crossing (javascript)
Browse files Browse the repository at this point in the history
  • Loading branch information
DhanushNehru committed Dec 23, 2023
1 parent e1ef27d commit f7ffc70
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions problems/path_crossing/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @param {string} path
* @return {boolean}
*/
var isPathCrossing = function(path) {
// Initialize starting position and a set to store visited points
let x = 0, y = 0, point = `${x} ${y}`;
const set = new Set([point])

for(direction of path){
// Update position based on the current direction
if(direction == "N"){
++x;
}
else if(direction == "E"){
++y;
}
else if(direction == "W"){
--y;
}
else if(direction == "S"){
--x
}

point = `${x} ${y}`;
// Check if the point has been visited before
if(set.has(point)){
return true
}
else{
// If the point is not visited, add it to the set
set.add(point)
}
}
// If no crossing occurred during the entire path, return false
return false
};

0 comments on commit f7ffc70

Please sign in to comment.