Determine whether a walk on the integer grid ever revisits a point.
You start at the origin on an infinite 2D integer grid. A route is given as a string of moves, where each character represents one step:
N : move up by 1S : move down by 1E : move right by 1W : move left by 1Your task is to determine whether the path ever crosses itself, meaning that some coordinate is visited more than once, including the starting point.
Return true if any position is visited at least twice; otherwise return false.
path containing only the characters N, S, E, and W.1 <= path.length <= $10^{4}$path is one of {N, S, E, W}path: a string of moves using N, S, E, W.true if the route visits any coordinate more than once; otherwise false.1 <= path.length <= $10^{4}$path[i] ∈ {N, S, E, W}Example 1
Input
path = "NES"
Output
false
Explanation
The path visits (0,0) -> (0,1) -> (1,1) -> (1,0). No coordinate is visited twice.
Example 2
Input
path = "NESWW"
Output
true
Explanation
The path visits (0,0) -> (0,1) -> (1,1) -> (1,0) -> (0,0) -> (-1,0). The origin is visited again, so the path crosses itself.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.