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.
Example 2:
Input: path = "NESWW" Output: true Explanation: Notice that the path visits the origin twice.
Constraints:
1 <= path.length <= 104path[i] is either 'N', 'S', 'E', or 'W'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem involves simulating every possible path the traveler could take based on the given directions. We'll keep track of every place the traveler visits. If the traveler ever revisits a location, the path crosses itself.
Here's how the algorithm would work step-by-step:
def path_crossing_brute_force(path):
current_location_x = 0
current_location_y = 0
visited_locations = set()
visited_locations.add((current_location_x, current_location_y))
for direction in path:
if direction == 'N':
current_location_y += 1
elif direction == 'S':
current_location_y -= 1
elif direction == 'E':
current_location_x += 1
else:
current_location_x -= 1
#Check if we have revisited this location
if (current_location_x, current_location_y) in visited_locations:
return True
#Add current location to the visited set
visited_locations.add((current_location_x, current_location_y))
#If we never revisit return false
return FalseWe need to figure out if a path ever crosses itself. We can do this by keeping track of every place the path has been. If the path ever revisits a place, then it has crossed itself.
Here's how the algorithm would work step-by-step:
def is_path_crossing(path):
x_coordinate = 0
y_coordinate = 0
visited_positions = set()
visited_positions.add((0, 0))
for move in path:
if move == 'N':
y_coordinate += 1
elif move == 'S':
y_coordinate -= 1
elif move == 'E':
x_coordinate += 1
else:
x_coordinate -= 1
new_position = (x_coordinate, y_coordinate)
# Check if the new position has already been visited.
if new_position in visited_positions:
return True
# Add the new position to the set of visited positions.
visited_positions.add(new_position)
return False| Case | How to Handle |
|---|---|
| Null or empty input string | Return True immediately as the starting point is considered visited; no path is taken to cross itself. |
| String of length 1 | Return False immediately as only one move is made and a crossing requires at least two moves. |
| Very long input string | The solution uses a set which has O(1) lookups for visited locations so performance remains acceptable even for long paths; memory usage scales linearly. |
| Path returns to origin immediately | The set of visited coordinates immediately detects the duplicate and returns True. |
| Path crosses itself far away from the origin | The set will eventually contain this coordinate again causing the algorithm to return True. |
| Path moves only in one direction | The algorithm correctly tracks the path and never returns True as no coordinate is ever revisited. |
| Integer overflow in coordinate calculations with very long paths | Consider using a larger integer type (e.g., long) or clamping the coordinates to prevent overflows, or scaling down coordinates to keep them within reasonable bounds. |
| Path attempts to revisit Integer.MAX_VALUE, Integer.MIN_VALUE coordinates. | The set handles arbitrarily large integer values in coordinates if memory allows, otherwise clamping or scaling as per the integer overflow edge case applies. |