3212. Count Submatrices With Equal Frequency of X and Y
</> Solution
/**
* Problem: LeetCode 3212 - Count Submatrices With Equal Frequency of X and Y
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int numberOfSubmatrices(char[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[] colX = new int[n];
int[] colY = new int[n];
int count = 0;
for (int i = 0; i < m; i++) {
int currentX = 0;
int currentY = 0;
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'X') {
colX[j]++;
} else if (grid[i][j] == 'Y') {
colY[j]++;
}
currentX += colX[j];
currentY += colY[j];
if (currentX == currentY && currentX > 0) {
count++;
}
}
}
return count;
}
}
"""
Problem: LeetCode 3212 - Count Submatrices With Equal Frequency of X and Y
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def numberOfSubmatrices(self, grid: list[list[str]]) -> int:
m, n = len(grid), len(grid[0])
col_x = [0] * n
col_y = [0] * n
count = 0
for i in range(m):
current_x = 0
current_y = 0
for j in range(n):
if grid[i][j] == 'X':
col_x[j] += 1
elif grid[i][j] == 'Y':
col_y[j] += 1
current_x += col_x[j]
current_y += col_y[j]
if current_x == current_y and current_x > 0:
count += 1
return count
/**
* Problem: LeetCode 3212 - Count Submatrices With Equal Frequency of X and Y
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
using namespace std;
class Solution {
public:
int numberOfSubmatrices(vector<vector<char>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<int> colX(n, 0);
vector<int> colY(n, 0);
int count = 0;
for (int i = 0; i < m; i++) {
int currentX = 0;
int currentY = 0;
for (int j = 0; j < n; j++) {
if (grid[i][j] == 'X') {
colX[j]++;
} else if (grid[i][j] == 'Y') {
colY[j]++;
}
currentX += colX[j];
currentY += colY[j];
if (currentX == currentY && currentX > 0) {
count++;
}
}
}
return count;
}
};
/**
* Problem: LeetCode 3212 - Count Submatrices With Equal Frequency of X and Y
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var numberOfSubmatrices = function(grid) {
const m = grid.length;
const n = grid[0].length;
const colX = new Int32Array(n);
const colY = new Int32Array(n);
let count = 0;
for (let i = 0; i < m; i++) {
let currentX = 0;
let currentY = 0;
for (let j = 0; j < n; j++) {
if (grid[i][j] === 'X') {
colX[j]++;
} else if (grid[i][j] === 'Y') {
colY[j]++;
}
currentX += colX[j];
currentY += colY[j];
if (currentX === currentY && currentX > 0) {
count++;
}
}
}
return count;
};