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”.
Input: str = “NESW”
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
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
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.
- Check if possible to move from given coordinate to desired coordinate
- Check whether one root of the Quadratic Equation is twice of other or not
- Check if there exists any sub-sequence in a string which is not palindrome
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- k-th missing element in increasing sequence which is not present in a given sequence
- Check if a given number is one less than twice its reverse
- Check if Euler Totient Function is same for a given number and twice of that number
- Number of possible Triangles in a Cartesian coordinate system
- Finding Quadrant of a Coordinate with respect to a Circle
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Check if any permutation of N equals any power of K
- Check if any K ranges overlap at any point
- Maximum in array which is at-least twice of other elements
- Count binary strings with twice zeros in first half
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Count of ways to represent N as sum of a prime number and twice of a square
- Count of pairs from Array with sum equal to twice their bitwise AND
- Count of elements to be inserted to make Array sum twice the XOR of Array
- Sum of first N natural numbers with all powers of 2 added twice
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.