Design Gurus Logo
Path Crossing (easy)

Problem Statement

You are given a string path, where path[i] = 'N', 'S', 'E' or 'W',, each representing the movement of one unit in North, South, East, and West direction, respectively. You start the journey on the 2D plane from origin (0, 0), and move towards the directions given in path string.

Return true, if your path crosses itself at any point, that is, if you ever visit the same point twice. Otherwise, return false.

Examples

  • Example 1:

    • Input: "NESW"
    • Expected Output: true
    • Justification: The path moves north, east, south, and then west, returning to the origin. This forms a loop, crossing at the starting point.
  • Example 2:

    • Input: "NNWWSS"
    • Expected Output: false
    • Justification: The path moves north twice, west twice, and south twice. It forms a rectangle and ends two units west of the origin without crossing any previous point.
  • Example 3:

    • Input: "NESWWN"
    • Expected Output: true
    • Justification: The path moves north, east, south, west twice, and then north, crossing its own path on the second west move.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory