A courier drone starts at cell (0, 0) and walks a path across an infinite integer grid. It reads a string of single-step moves, one character at a time:
N: y-coordinate + 1,S: y-coordinate - 1,E: x-coordinate + 1,W: x-coordinate - 1.The starting cell counts as visited. The path self-intersects if, at any moment after a move, the drone lands on a cell it has already visited (including a return to the origin).
A single line: a non-empty string of characters, each one of N, S, E, W.
Print YES if the path ever revisits a cell, otherwise print NO.
Example 1
Input
NESW
Expected
YES
Explanation
The path visits (0,0), (0,1), (1,1), (1,0), then returns to (0,0), which was already visited, so it self-intersects.
Example 2
Input
NNE
Expected
NO
Explanation
The path visits (0,0), (0,1), (0,2), (1,2), all distinct, so it never revisits a cell.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →