Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given a 2D character matrix grid, where grid[i][j] is either 'X', 'Y', or '.', return the number of submatrices that contain:
grid[0][0]'X' and 'Y'.'X'.Example 1:
Input: grid = [["X","Y","."],["Y",".","."]]
Output: 3
Explanation:
Example 2:
Input: grid = [["X","X"],["X","Y"]]
Output: 0
Explanation:
No submatrix has an equal frequency of 'X' and 'Y'.
Example 3:
Input: grid = [[".","."],[".","."]]
Output: 0
Explanation:
No submatrix has at least one 'X'.
Constraints:
1 <= grid.length, grid[i].length <= 1000grid[i][j] is either 'X', 'Y', or '.'.Problem summary: Given a 2D character matrix grid, where grid[i][j] is either 'X', 'Y', or '.', return the number of submatrices that contain: grid[0][0] an equal frequency of 'X' and 'Y'. at least one 'X'.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[["X","Y","."],["Y",".","."]]
[["X","X"],["X","Y"]]
[[".","."],[".","."]]
maximum-equal-frequency)count-submatrices-with-all-ones)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3212: Count Submatrices With Equal Frequency of X and Y
class Solution {
public int numberOfSubmatrices(char[][] grid) {
int m = grid.length, n = grid[0].length;
int[][][] s = new int[m + 1][n + 1][2];
int ans = 0;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0]
+ (grid[i - 1][j - 1] == 'X' ? 1 : 0);
s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1]
+ (grid[i - 1][j - 1] == 'Y' ? 1 : 0);
if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3212: Count Submatrices With Equal Frequency of X and Y
func numberOfSubmatrices(grid [][]byte) (ans int) {
m, n := len(grid), len(grid[0])
s := make([][][]int, m+1)
for i := range s {
s[i] = make([][]int, n+1)
for j := range s[i] {
s[i][j] = make([]int, 2)
}
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
s[i][j][0] = s[i-1][j][0] + s[i][j-1][0] - s[i-1][j-1][0]
if grid[i-1][j-1] == 'X' {
s[i][j][0]++
}
s[i][j][1] = s[i-1][j][1] + s[i][j-1][1] - s[i-1][j-1][1]
if grid[i-1][j-1] == 'Y' {
s[i][j][1]++
}
if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #3212: Count Submatrices With Equal Frequency of X and Y
class Solution:
def numberOfSubmatrices(self, grid: List[List[str]]) -> int:
m, n = len(grid), len(grid[0])
s = [[[0] * 2 for _ in range(n + 1)] for _ in range(m + 1)]
ans = 0
for i, row in enumerate(grid, 1):
for j, x in enumerate(row, 1):
s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0]
s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1]
if x != ".":
s[i][j][ord(x) & 1] += 1
if s[i][j][0] > 0 and s[i][j][0] == s[i][j][1]:
ans += 1
return ans
// Accepted solution for LeetCode #3212: Count Submatrices With Equal Frequency of X and Y
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3212: Count Submatrices With Equal Frequency of X and Y
// class Solution {
// public int numberOfSubmatrices(char[][] grid) {
// int m = grid.length, n = grid[0].length;
// int[][][] s = new int[m + 1][n + 1][2];
// int ans = 0;
// for (int i = 1; i <= m; ++i) {
// for (int j = 1; j <= n; ++j) {
// s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0]
// + (grid[i - 1][j - 1] == 'X' ? 1 : 0);
// s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1]
// + (grid[i - 1][j - 1] == 'Y' ? 1 : 0);
// if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) {
// ++ans;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3212: Count Submatrices With Equal Frequency of X and Y
function numberOfSubmatrices(grid: string[][]): number {
const [m, n] = [grid.length, grid[0].length];
const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => [0, 0]));
let ans = 0;
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
s[i][j][0] =
s[i - 1][j][0] +
s[i][j - 1][0] -
s[i - 1][j - 1][0] +
(grid[i - 1][j - 1] === 'X' ? 1 : 0);
s[i][j][1] =
s[i - 1][j][1] +
s[i][j - 1][1] -
s[i - 1][j - 1][1] +
(grid[i - 1][j - 1] === 'Y' ? 1 : 0);
if (s[i][j][0] > 0 && s[i][j][0] === s[i][j][1]) {
++ans;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.