3212.cpp (684B)
1 class Solution { 2 public: 3 int numberOfSubmatrices(const vector<vector<char>> &grid) const { 4 const int n = size(grid), m = size(grid[0]); 5 vector<vector<int>> x(n + 1, vector(m + 1, 0)); 6 vector<vector<int>> y(n + 1, vector(m + 1, 0)); 7 int res = 0; 8 9 for (int i = 1; i <= n; i++) { 10 for (int j = 1; j <= m; j++) { 11 x[i][j] = x[i - 1][j] + x[i][j - 1] - x[i - 1][j - 1] + (grid[i - 1][j - 1] == 'X'); 12 y[i][j] = y[i - 1][j] + y[i][j - 1] - y[i - 1][j - 1] + (grid[i - 1][j - 1] == 'Y'); 13 if (x[i][j] > 0 && x[i][j] == y[i][j]) res++; 14 } 15 } 16 17 return res; 18 } 19 };