Leetcode #999: Available Captures for Rook
In this guide, we solve Leetcode #999 Available Captures for Rook in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
You are given an 8 x 8 matrix representing a chessboard. There is exactly one white rook represented by 'R', some number of white bishops 'B', and some number of black pawns 'p'.
Quick Facts
- Difficulty: Easy
- Premium: No
- Tags: Array, Matrix, Simulation
Intuition
Grid problems are easiest when you define clear row/column boundaries.
A consistent traversal order prevents off-by-one errors.
Approach
Iterate by rows, columns, or layers depending on the requirement.
Keep bounds updated as the traversal progresses.
Steps:
- Define bounds or directions.
- Visit cells in order.
- Update result and move bounds.
Python Solution
class Solution:
def numRookCaptures(self, board: List[List[str]]) -> int:
dirs = (-1, 0, 1, 0, -1)
n = len(board)
for i in range(n):
for j in range(n):
if board[i][j] == "R":
ans = 0
for a, b in pairwise(dirs):
x, y = i + a, j + b
while 0 <= x < n and 0 <= y < n and board[x][y] != "B":
if board[x][y] == "p":
ans += 1
break
x, y = x + a, y + b
return ans
Complexity
The time complexity is , where and are the number of rows and columns of the board, respectively. The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.