Skip to content

Practice Path Crossing

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1496
LeetCode Problem

Path Crossing

Problem Statement

Given a string path, where path[i] = 'N', 'S', 'E' or 'W', each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0) on a 2D plane and walk on the path specified by path. Return true if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false otherwise.

Source: Path Crossing on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
path = "NES"
Output
false

# Example 2

Input
path = "NESWW"
Output
true

Constraints

1 <= path.length <= 10^4
path[i] is either 'N', 'S', 'E', or 'W'.

Related LeetCode Problems

Practice more problems similar to Path Crossing.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.