Skip to the code
cf.
+10 XP

#26 Drag Path Crosses Itself

easy

A drawing app records a mouse drag as a string path of unit moves on a grid: N (y + 1), S (y - 1), E (x + 1), and W (x - 1). The drag starts at (0, 0).

Write pathCrosses(path) that returns true if the drag ever visits the same point twice (including the starting point), and false otherwise.

Example 1

Input: path = "NESW"
Output: true

The last move comes back to (0, 0).

Example 2

Input: path = "NES"
Output: false

The points (0, 1), (1, 1), and (1, 0) are all new.

Constraints

  • 0 <= path.length <= 10^4
  • path has only N, S, E, W.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).