3212. Count Submatrices With Equal Frequency of X and Y LeetCode Solution
In this guide, you will get 3212. Count Submatrices With Equal Frequency of X and Y LeetCode Solution with the best time and space complexity. The solution to Count Submatrices With Equal Frequency of X and Y problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Submatrices With Equal Frequency of X and Y solution in C++
- Count Submatrices With Equal Frequency of X and Y solution in Java
- Count Submatrices With Equal Frequency of X and Y solution in Python
- Additional Resources

Problem Statement of Count Submatrices With Equal Frequency of X and Y
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’.
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 <= 1000
grid[i][j] is either 'X', 'Y', or '.'.
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
3212. Count Submatrices With Equal Frequency of X and Y LeetCode Solution in C++
class Solution {
public:
int numberOfSubmatrices(vector<vector<char>>& grid) {
const int m = grid.size();
const int n = grid[0].size();
int ans = 0;
// x[i][j] := the number of 'X' in grid[0..i)[0..j)
vector<vector<int>> x(m + 1, vector<int>(n + 1));
// y[i][j] := the number of 'Y' in grid[0..i)[0..j)
vector<vector<int>> y(m + 1, vector<int>(n + 1));
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
x[i + 1][j + 1] =
(grid[i][j] == 'X' ? 1 : 0) + x[i][j + 1] + x[i + 1][j] - x[i][j];
y[i + 1][j + 1] =
(grid[i][j] == 'Y' ? 1 : 0) + y[i][j + 1] + y[i + 1][j] - y[i][j];
if (x[i + 1][j + 1] > 0 && x[i + 1][j + 1] == y[i + 1][j + 1])
++ans;
}
return ans;
}
};
/* code provided by PROGIEZ */
3212. Count Submatrices With Equal Frequency of X and Y LeetCode Solution in Java
class Solution {
public int numberOfSubmatrices(char[][] grid) {
final int m = grid.length;
final int n = grid[0].length;
int ans = 0;
// x[i][j] := the number of 'X' in grid[0..i)[0..j)
int[][] x = new int[m + 1][n + 1];
// y[i][j] := the number of 'Y' in grid[0..i)[0..j)
int[][] y = new int[m + 1][n + 1];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
x[i + 1][j + 1] = (grid[i][j] == 'X' ? 1 : 0) + x[i][j + 1] + x[i + 1][j] - x[i][j];
y[i + 1][j + 1] = (grid[i][j] == 'Y' ? 1 : 0) + y[i][j + 1] + y[i + 1][j] - y[i][j];
if (x[i + 1][j + 1] > 0 && x[i + 1][j + 1] == y[i + 1][j + 1])
++ans;
}
return ans;
}
}
// code provided by PROGIEZ
3212. Count Submatrices With Equal Frequency of X and Y LeetCode Solution in Python
class Solution:
def numberOfSubmatrices(self, grid: list[list[str]]) -> int:
m = len(grid)
n = len(grid[0])
ans = 0
# x[i][j] := the number of 'X' in grid[0..i)[0..j)
x = [[0] * (n + 1) for _ in range(m + 1)]
# y[i][j] := the number of 'Y' in grid[0..i)[0..j)
y = [[0] * (n + 1) for _ in range(m + 1)]
for i, row in enumerate(grid):
for j, cell in enumerate(row):
x[i + 1][j + 1] = (cell == 'X') + x[i][j + 1] + x[i + 1][j] - x[i][j]
y[i + 1][j + 1] = (cell == 'Y') + y[i][j + 1] + y[i + 1][j] - y[i][j]
if x[i + 1][j + 1] > 0 and x[i + 1][j + 1] == y[i + 1][j + 1]:
ans += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.