Given string str of length N only consisting of characters ‘N’, ‘S’, ‘E’, or ‘W’, each representing moving one unit North, South, East, or West, respectively. A man starts at the origin (0, 0) on a 2D plane and walks according to the directions in the string. The task is to check whether the man crosses any coordinate in his walk, which he has already visited or not. If found to be true, then print “Crossed”. Otherwise, print “Not Crossed”.
Examples:
Input: str = “NESW”
Output: Crossed
Explanation:
The path for the man is given by:
(0, 0) -> (0, 1) -> (1, 1) -> (0, 1) -> (0, 0).
Since, the coordinate (0, 0) is visited again.
Therefore, print Crossed as he has crossed the path.Input: str = “SESS”
Output: Not Crossed
Explanation:
The path for the man is given by:
(0, 0) -> (0, -1) -> (1, -1) -> (1, -2) -> (1, -3).
From the above path, the man never visited the same coordinate again.
Therefore, print Not Crossed.
Approach: The idea is to maintain a Set for the co-ordinates encountered to check whether already visited co-ordinates occurs or not. Below are the steps:
- Initialize two variables X and Y and initiate the values as zero. These variables will represent x and y coordinates.
- Insert X and Y in the set.
- Iterate a loop over the range [0, N), and increment the X or Y coordinate as per the below condition:
- If the character is ‘N’, then increment Y by 1.
- If the character is ‘S’, then decrement Y by 1.
- If the character is ‘E’, then increment X by 1.
- If the character is ‘W’, then decrement X by 1.
- Insert the updated X and Y coordinates in the above step for each character in the set.
- While inserting the co-ordinates in the above step if any present co-ordinates are encountered then print Crossed.
- Otherwise, print Not Crossed as all the points of co-ordinates are unique.
Below is the implementation of the above approach
C++
// C++ program for the above appproach#include <bits/stdc++.h>using namespace std;// Function to check if the man crosses// previous visited coordinate or notbool isCrossed(string path){ if (path.size() == 0) return false; // Stores the count of // crossed vertex bool ans = false; // Stores (x, y) coordinates set<pair<int, int> > set; // The coordinates for the origin int x = 0, y = 0; set.insert({ x, y }); // Iterate over the string for (int i = 0; i < path.size(); i++) { // Condition to increment X or // Y co-ordinates respectively if (path[i] == 'N') set.insert({ x, y++ }); if (path[i] == 'S') set.insert({ x, y-- }); if (path[i] == 'E') set.insert({ x++, y }); if (path[i] == 'W') set.insert({ x--, y }); // Check if (x, y) is already // visited if (set.find({ x, y }) != set.end()) { ans = true; break; } } // Print the result if (ans) cout << "Crossed"; else cout << "Not Crossed";}// Driver Codeint main(){ // Given string string path = "NESW"; // Function Call isCrossed(path); return 0;} |
Java
// Java program for // the above appproachimport java.awt.Point;import java.util.HashSet;class GFG{// Function to check if the man crosses// previous visited coordinate or notstatic void isCrossed(String path){ if (path.length() == 0) return; // Stores the count of // crossed vertex boolean ans = false; // Stores (x, y) coordinates HashSet<Point> set = new HashSet<Point>(); // The coordinates for the origin int x = 0, y = 0; set.add(new Point(x, y)); // Iterate over the String for (int i = 0; i < path.length(); i++) { // Condition to increment X or // Y co-ordinates respectively if (path.charAt(i) == 'N') set.add(new Point(x, y++)); if (path.charAt(i) == 'S') set.add(new Point(x, y--)); if (path.charAt(i) == 'E') set.add(new Point(x++, y)); if (path.charAt(i) == 'W') set.add(new Point(x--, y)); // Check if (x, y) is already // visited if (set.contains(new Point(x, y))) { ans = true; break; } } // Print the result if (ans) System.out.print("Crossed"); else System.out.print("Not Crossed");}// Driver Codepublic static void main(String[] args){ // Given String String path = "NESW"; // Function Call isCrossed(path);}}// This code is contributed by Princi Singh |
Python3
# Python3 program for the above appproach# Function to check if the man crosses# previous visited coordinate or notdef isCrossed(path): if (len(path) == 0): return bool(False) # Stores the count of # crossed vertex ans = bool(False) # Stores (x, y) coordinates Set = set() # The coordinates for the origin x, y = 0, 0 Set.add((x, y)) # Iterate over the string for i in range(len(path)): # Condition to increment X or # Y co-ordinates respectively if (path[i] == 'N'): Set.add((x, y)) y = y + 1 if (path[i] == 'S'): Set.add((x, y)) y = y - 1 if (path[i] == 'E'): Set.add((x, y)) x = x + 1 if (path[i] == 'W'): Set.add((x, y)) x = x - 1 # Check if (x, y) is already visited if (x, y) in Set: ans = bool(True) break # Print the result if (ans): print("Crossed") else: print("Not Crossed")# Driver code# Given stringpath = "NESW"# Function callisCrossed(path)# This code is contributed by divyeshrabadiya07 |
C#
// C# program for the above appproachusing System;using System.Collections.Generic;class GFG{// Function to check if the man crosses// previous visited coordinate or notstatic void isCrossed(String path){ if (path.Length == 0) return; // Stores the count of // crossed vertex bool ans = false; // Stores (x, y) coordinates HashSet<KeyValuePair<int, int>> mySet = new HashSet<KeyValuePair<int, int>>(); // The coordinates for the origin int x = 0, y = 0; mySet.Add(new KeyValuePair<int, int>(x, y)); // Iterate over the String for(int i = 0; i < path.Length; i++) { // Condition to increment X or // Y co-ordinates respectively if (path[i] == 'N') mySet.Add( new KeyValuePair<int, int>(x, y++)); if (path[i] == 'S') mySet.Add( new KeyValuePair<int, int>(x, y--)); if (path[i] == 'E') mySet.Add( new KeyValuePair<int, int>(x++, y)); if (path[i] == 'W') mySet.Add( new KeyValuePair<int, int>(x--, y)); // Check if (x, y) is already // visited if (mySet.Contains( new KeyValuePair<int, int>(x, y))) { ans = true; break; } } // Print the result if (ans) Console.Write("Crossed"); else Console.Write("Not Crossed");}// Driver Codepublic static void Main(String[] args){ // Given String String path = "NESW"; // Function call isCrossed(path);}}// This code is contributed by akhilsaini |
Crossed
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

