|
| 1 | +# [Problem 3212: Count Submatrices With Equal Frequency of X and Y](https://leetcode.com/problems/count-submatrices-with-equal-frequency-of-x-and-y/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We need to count submatrices that (1) contain grid[0][0], (2) have equal counts of 'X' and 'Y', and (3) contain at least one 'X'. "Contain grid[0][0]" means the submatrix must include the cell (0,0). For a submatrix (contiguous rows and columns) to include (0,0) and be valid indices, its top row and left column must be 0. So every valid submatrix is actually a prefix submatrix anchored at (0,0) with some bottom-right corner (i, j). That simplifies the problem: we only need to check the rectangle from (0,0) to (i,j) for all i, j. |
| 5 | + |
| 6 | +So for each cell (i,j) compute the counts of 'X' and 'Y' in the prefix rectangle (0,0)-(i,j). Count how many such prefixes have countX == countY and countX >= 1. |
| 7 | + |
| 8 | +We can compute prefix counts efficiently with a per-row running sum plus adding the previous-row prefix sums, keeping only O(n) space. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +- Because top-left is fixed to (0,0), there are m * n candidate submatrices only, so an O(m * n) solution is ideal and easy to achieve. |
| 12 | +- Compute prefix counts for 'X' and 'Y' for each (i, j) using running row sums plus previous-row prefix totals. |
| 13 | +- Edge cases: |
| 14 | + - All dots ('.'): equal counts (0 == 0) but must have at least one 'X' — so answer should be 0. |
| 15 | + - Very large grid up to 1000x1000: O(mn) time and O(n) extra space is fine. |
| 16 | +- No need for hashing or complicated transforms. |
| 17 | + |
| 18 | +## Attempted solution(s) |
| 19 | +```python |
| 20 | +from typing import List |
| 21 | + |
| 22 | +class Solution: |
| 23 | + def countSubmatrices(self, grid: List[List[str]]) -> int: |
| 24 | + """ |
| 25 | + Count prefix submatrices anchored at (0,0) whose counts of 'X' and 'Y' are equal |
| 26 | + and contain at least one 'X'. |
| 27 | + """ |
| 28 | + if not grid or not grid[0]: |
| 29 | + return 0 |
| 30 | + m, n = len(grid), len(grid[0]) |
| 31 | + # prevX[j] and prevY[j] store the prefix sums for rows 0..i-1 and cols 0..j |
| 32 | + prevX = [0] * n |
| 33 | + prevY = [0] * n |
| 34 | + ans = 0 |
| 35 | + |
| 36 | + for i in range(m): |
| 37 | + runningX = 0 |
| 38 | + runningY = 0 |
| 39 | + prefX = [0] * n |
| 40 | + prefY = [0] * n |
| 41 | + for j in range(n): |
| 42 | + ch = grid[i][j] |
| 43 | + if ch == 'X': |
| 44 | + runningX += 1 |
| 45 | + elif ch == 'Y': |
| 46 | + runningY += 1 |
| 47 | + # prefix up to (i, j) = running sum in current row up to j + prefix of rows above up to j |
| 48 | + prefX[j] = runningX + (prevX[j] if i > 0 else 0) |
| 49 | + prefY[j] = runningY + (prevY[j] if i > 0 else 0) |
| 50 | + # check conditions: equal counts and at least one X |
| 51 | + if prefX[j] == prefY[j] and prefX[j] > 0: |
| 52 | + ans += 1 |
| 53 | + prevX = prefX |
| 54 | + prevY = prefY |
| 55 | + |
| 56 | + return ans |
| 57 | +``` |
| 58 | +- Notes: |
| 59 | + - We exploit that any submatrix containing (0,0) must be the prefix (0,0)-(i,j). |
| 60 | + - For each row i we maintain runningX and runningY (row cumulative sums up to column j). Adding prevX[j] / prevY[j] (sums from earlier rows up to col j) yields full prefix sums. |
| 61 | + - Time complexity: O(m * n) where m and n are grid dimensions. |
| 62 | + - Space complexity: O(n) extra space (two arrays prevX and prevY of length n, and O(n) temporary arrays prefX/prefY per row). |
0 commit comments