Skip to content

Commit a1fb94c

Browse files
Create Count Submatrices With Equal Frequency of X and Y.cpp
1 parent 05ccf0b commit a1fb94c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int numberOfSubmatrices(vector<vector<char>>& grid) {
4+
int r = grid.size();
5+
int c = grid[0].size();
6+
vector<vector<int>> prex(r+1, vector<int>(c+1, 0));
7+
vector<vector<int>> prey(r+1, vector<int>(c+1, 0));
8+
9+
for(int i = 1 ; i <= r ; i++){
10+
for(int j = 1 ; j <= c ; j++){
11+
prex[i][j] = prex[i-1][j] + prex[i][j-1] - prex[i-1][j-1] + (grid[i-1][j-1]=='X' ? 1 : 0);
12+
prey[i][j] = prey[i-1][j] + prey[i][j-1] - prey[i-1][j-1] + (grid[i-1][j-1]=='Y' ? 1 : 0);
13+
}
14+
}
15+
int res = 0;
16+
for(int i = 0 ; i < r ; i++){
17+
for(int j = 0 ; j < c ; j++){
18+
int cx = prex[i+1][j+1] - prex[0][j+1] - prex[i+1][0] + prex[0][0];
19+
int cy = prey[i+1][j+1] - prey[0][j+1] - prey[i+1][0] + prey[0][0];
20+
if(cx == cy and cx > 0){
21+
res++;
22+
}
23+
}
24+
}
25+
return res;
26+
}
27+
};

0 commit comments

Comments
 (0)