Check if Move is Legal
Medium · Arrays & Hashing · Explanation and code example
Iteration - II
How it works
This is a more compact implementation of the same logic. Instead of storing directions as pairs, we use a single array where consecutive elements form direction pairs. This reduces memory usage slightly and makes the iteration more streamlined.
Steps
- Place the piece of the given color at the specified position.
- Use a compact direction array where
direction[d]anddirection[d+1]represent the row and column deltas for each direction. - Loop through indices
0to8, treating each as a different direction. - For each direction:
- Start from the placed position.
- Move in the direction while tracking the line length.
- Stop if we go out of bounds or hit an empty cell.
- If we reach our own color and have traversed more than one cell (length > 1), return
true.
- If no valid line is found in any direction, return
false.
Complexity
- Time complexity:
O(1) - Space complexity:
O(1)
Things to watch for
Forgetting to Place the Piece Before Checking
The move validation must include the piece being placed at (rMove, cMove). If you forget to set board[rMove][cMove] = color before checking directions, you may incorrectly validate lines that don't actually start with your piece.
Not Requiring At Least One Opponent Piece Between
A valid line must have at least one opponent piece between your placed piece and another piece of your color. Checking only that the endpoint matches your color without verifying length >= 3 allows invalid moves where two of your pieces are adjacent.
Missing a Direction or Checking Only 4 Directions
There are 8 possible directions (horizontal, vertical, and 4 diagonals). A common mistake is checking only 4 directions (e.g., only cardinal or only diagonal), which misses valid lines in the unchecked directions.
Python code example
from typing import List
class Solution:
def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool:
ROWS, COLS = len(board), len(board[0])
direction = [0, 1, 0, -1, 0, 1, 1, -1, -1, 1]
board[rMove][cMove] = color
for d in range(9):
length = 1
row, col = rMove, cMove
while True:
row += direction[d]
col += direction[d + 1]
if row < 0 or col < 0 or row >= ROWS or col >= COLS or board[row][col] == ".":
break
if board[row][col] == color:
if length > 1:
return True
break
length += 1
return False
C++ code example
class Solution {
public:
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
int ROWS = board.size(), COLS = board[0].size();
int direction[10] = {0, 1, 0, -1, 0, 1, 1, -1, -1, 1};
board[rMove][cMove] = color;
for (int d = 0; d < 9; ++d) {
int row = rMove, col = cMove;
for (int length = 1; ; ++length) {
row += direction[d];
col += direction[d + 1];
if (row < 0 || col < 0 || row >= ROWS || col >= COLS || board[row][col] == '.')
break;
if (board[row][col] == color) {
if (length > 1)
return true;
break;
}
}
}
return false;
}
};
Source and license
Adapted from NeetCode’s source · MIT license. Node types are supplied by LeetCode where needed.