Skip to content

Commit 186dc95

Browse files
committed
Add path crossing
1 parent a6aa486 commit 186dc95

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ List of Programs related to data structures and algorithms
212212
| 9 | LRU Cache | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/9.lruCache/lruCache.md) | Medium | Combination of Hash Table and doubly linked list |
213213
| 10 | Maximum number of balloons | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/10.maximumBalloons/maximumBalloons.md) | Easy | Hash map on characters |
214214
| 11 | Isomorphic Strings | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/11.isomorphicStrings/isomorphicStrings.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/11.isomorphicStrings/isomorphicStrings.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/11.isomorphicStrings/isomorphicStrings.md) | Easy | mapping characters using Hash maps |
215+
| 12 | Path Crossing | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/12.pathCrossing/pathCrossing.js) | [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/12.pathCrossing/pathCrossing.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/hashtable/12.pathCrossing/pathCrossing.md) | Easy | Visited set based on path characters |
215216

216217
## Sorting
217218

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package java1.algorithms.hashmap.pathCrossing;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class PathCrossing {
7+
private static boolean isPathCrossing(String path) {
8+
Set<String> visited = new HashSet<>();
9+
int x=0, y = 0;
10+
11+
visited.add(x+","+y);
12+
13+
for (char move : path.toCharArray()) {
14+
if(move == 'N') y++;
15+
if(move == 'S') y--;
16+
if(move == 'E') x++;
17+
if(move == 'W') x--;
18+
19+
if(visited.contains(x+","+y)) {
20+
return true;
21+
}
22+
23+
visited.add(x+","+y);
24+
}
25+
26+
return false;
27+
}
28+
29+
public static void main(String[] args) {
30+
String path1="NESE";
31+
String path2="NEWS";
32+
33+
System.out.println(isPathCrossing(path1));
34+
System.out.println(isPathCrossing(path2));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
**Problem statement:**
2+
Given a string `path`, where `path[i] = 'N', 'S', 'E' or 'W'`, each letter representing moving one unit towards north, south, east, or west, respectively. Assume that you started at the origin (0, 0) and walk on the specified path(`path`).
3+
4+
Return `true` if the path crosses same point at any point. otherwise, return `false`.
5+
6+
## Examples:
7+
Example 1:
8+
9+
Input: path = "NESE"
10+
11+
Output: false
12+
13+
14+
Example 2:
15+
16+
Input: path = "NESW"
17+
18+
Output: true
19+
20+
21+
**Algorithmic Steps**
22+
This problem is solved with the help of a set, which is used to store the visited co-ordinates and determine if the current point is crossed or not.
23+
24+
1. Define a set variable(`visited`) to keep track of visited co-oridnates.
25+
26+
2. Initialize current co-ordinates to (0,0).
27+
28+
3. Iterate over each character of given path,
29+
1. Update the current co-ordinates based on each character(`N, E, W, S`).
30+
2. If the updated location is already visited, return true indicating that path is already crossed.
31+
3. Add the newly visited co-ordinate/point to the visited set.
32+
33+
4. Return `false` indicating that path didn't cross the same point.
34+
35+
**Time and Space complexity:**
36+
This algorithm has a time complexity of `O(n)`, Where `n` is the length of the path. This is because we traverse the string once and perform constant time operations for lookup in a set.
37+
38+
Here, it takes time complexity of `O(n)`. This is because of the space required by `visited` hash set.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function isPathCrossing(path){
2+
const visited = new Set();
3+
4+
let x = 0, y = 0;
5+
visited.add(`${x},${y}`);
6+
7+
const directions = {
8+
'N': [0, 1],
9+
'S': [0, -1],
10+
'E': [1, 0],
11+
'W': [-1, 0]
12+
}
13+
14+
for (const move of path) {
15+
x+= directions[move][0],
16+
y+= directions[move][1]
17+
18+
if(visited.has(`${x},${y}`)) {
19+
return true;
20+
}
21+
22+
visited.add(`${x},${y}`);
23+
}
24+
25+
return false;
26+
}
27+
28+
const path1="NESE";
29+
const path2="NEWS";
30+
31+
console.log(isPathCrossing(path1));
32+
console.log(isPathCrossing(path2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**Problem statement:**
2+
Given a string `path`, where `path[i] = 'N', 'S', 'E' or 'W'`, each letter representing moving one unit towards north, south, east, or west, respectively. Assume that you started at the origin (0, 0) and walk on the specified path(`path`).
3+
4+
Return `true` if the path crosses same point at any point. otherwise, return `false`.
5+
6+
## Examples:
7+
Example 1:
8+
9+
Input: path = "NESE"
10+
11+
Output: false
12+
13+
14+
Example 2:
15+
16+
Input: path = "NESW"
17+
18+
Output: true
19+
20+
21+
**Algorithmic Steps**
22+
This problem is solved with the help of a set, which is used to store the visited co-ordinates and determine if the current point is crossed or not.
23+
24+
1. Define a set variable(`visited`) to keep track of visited co-oridnates.
25+
26+
2. Initialize current co-ordinates to (0,0).
27+
28+
3. Declare a `directions` object to map the direction with the respective unit points
29+
30+
4. Iterate over each character of given path,
31+
1. Update the current co-ordinates based on each character.
32+
2. If the updated location is already visited, return true indicating that path is already crossed.
33+
3. Add the newly visited co-ordinate/point to the visited set.
34+
35+
5. Return `false` indicating that path didn't cross the same point.
36+
37+
**Time and Space complexity:**
38+
This algorithm has a time complexity of `O(n)`, Where `n` is the length of the path. This is because we traverse the string once and perform constant time operations for lookup in a set.
39+
40+
Here, it takes time complexity of `O(n)`. This is because of the space required by `visited` hash set.

0 commit comments

Comments
 (0)