Advertisement

Path Crossing - LeetCode 1496 Solution

Path Crossing - Complete Solution Guide

Path Crossing is LeetCode problem 1496, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp.

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. Example 1: Input: path = "NES" Output: false Explanation: Notice that the path doesn't cross any point more than once. Ex

Detailed Explanation

The problem asks whether a path, represented by a string of 'N', 'S', 'E', and 'W' characters (North, South, East, West), crosses itself. The path starts at the origin (0, 0) on a 2D plane. Each character in the string represents a movement of one unit in the corresponding direction. The path crosses itself if it visits the same coordinate more than once. The output should be a boolean value: `true` if the path crosses itself, and `false` otherwise.

Solution Approach

The provided solutions use a set (or hash set in Java, unordered set in C++) to store the visited coordinates as (x, y) pairs. The algorithm iterates through the path string: For each direction, it updates the x and y coordinates accordingly. Before adding the new coordinates to the set, it checks if the coordinates are already present. If a duplicate is found, it means the path has crossed itself, and the function immediately returns `true`. If the loop completes without finding any duplicates, it means the path does not cross itself, and the function returns `false`.

Step-by-Step Algorithm

  1. Step 1: Initialize a set `visited` to store the coordinates visited so far. Add the origin (0, 0) to `visited`.
  2. Step 2: Initialize the current coordinates `x` and `y` to 0.
  3. Step 3: Iterate through each character `c` in the input string `path`.
  4. Step 4: Based on the character `c`, update `x` and `y` accordingly ('N': y++, 'S': y--, 'E': x++, 'W': x--).
  5. Step 5: Check if the new coordinates `(x, y)` are already in the `visited` set. If they are, return `true` (path crossing detected).
  6. Step 6: Add the new coordinates `(x, y)` to the `visited` set.
  7. Step 7: After iterating through all characters, return `false` (no path crossing).

Key Insights

  • Insight 1: Using a set (or hash table) to efficiently track visited coordinates is crucial for achieving an optimal time complexity. Sets provide constant-time average complexity for insertion and lookup.
  • Insight 2: The problem can be solved by iterating through the path string, updating the current coordinates based on each direction, and checking if the new coordinates have already been visited. This requires keeping track of the coordinates' history.
  • Insight 3: The origin (0,0) is considered a visited point from the beginning, because the path starts there.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String.

Companies

Asked at: Yandex.