[
2d-array
simulation
]
Leetcode 0999. Available Captures for Rook
Problem statement
https://leetcode.com/problems/available-captures-for-rook/
Solution
First we need to find rook. Then let us create column and row for rook where we remove all empty cells. Finally, we loook for Rp
or pR
substrings in these row and column.
Complexity
Time complexity is O(64)
, space is O(8)
.
Code
class Solution:
def numRookCaptures(self, B):
y, x = next((i, j) for j in range(8) for i in range(8) if B[i][j] == 'R')
row = "".join(B[y][j] for j in range(8) if B[y][j] != ".")
col = "".join(B[i][x] for i in range(8) if B[i][x] != ".")
return sum(a in b for a in ('Rp', 'pR') for b in (row, col))