1008. Check if Path Crosses Itself

EasyGraphsGridHashGraph

Given a string path of moves where each character is 'N', 'S', 'E', or 'W' (starting at the origin), determine whether the path ever revisits a point it has already been to (including the origin). Return true if it crosses itself, else false. The input is JSON {path}.

Input: JSON {path}.

Output: Boolean — true if the path crosses itself.

Examples

Example 1
Input: {"path":"NES"}
Output: false
Explanation: No point revisited.
Example 2
Input: {"path":"NESWW"}
Output: true
Explanation: Returns to a visited point.
Example 3
Input: {"path":"N"}
Output: false
Explanation: Single move.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →