Leetcode Problem 1958. Check if Move is Legal

1958. Check if Move is Legal

Leetcode Solutions

Eight-Directional Endpoint Check

  1. Define the eight possible directions as pairs of row and column increments.
  2. For each direction, check the adjacent cell to the move cell to ensure it is of the opposite color.
  3. Continue moving in that direction, counting the number of opposite color cells until either a cell of the same color is found (indicating a potential 'good line') or a free cell or the edge of the board is reached.
  4. If a cell of the same color is found and at least one opposite color cell was seen in between, the move is legal for that direction.
  5. If a legal move is found in any direction, return true.
  6. If no legal moves are found after checking all directions, return false.
UML Thumbnail

Iterative Line Checking

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...