LeetCode 3212: Count Submatrices With Equal Frequency of X and Y Solution

Master LeetCode problem 3212 (Count Submatrices With Equal Frequency of X and Y), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3212. Count Submatrices With Equal Frequency of X and Y

Problem Explanation

Explanation

To solve this problem, we can iterate over all possible submatrices within the given grid. For each submatrix, we calculate the frequency of 'X' and 'Y' within that submatrix. If the frequency of 'X' and 'Y' is equal and there is at least one 'X', then we increment the count of valid submatrices.

We can optimize this process by using prefix sums to calculate the frequency of 'X' and 'Y' in each submatrix efficiently. We maintain two prefix sum arrays: one for the frequency of 'X' and one for the frequency of 'Y'. By comparing the prefix sums of 'X' and 'Y' for each submatrix, we can determine the validity of the submatrix.

The time complexity of this approach is O(N^3) where N is the size of the grid, and the space complexity is O(N^2).

Solution Code

class Solution {
    public int countSubmatrices(char[][] grid) {
        int rows = grid.length;
        int cols = grid[0].length;
        int count = 0;

        int[][] prefixX = new int[rows + 1][cols + 1];
        int[][] prefixY = new int[rows + 1][cols + 1];

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                prefixX[i][j] = prefixX[i - 1][j] + prefixX[i][j - 1] - prefixX[i - 1][j - 1] + (grid[i - 1][j - 1] == 'X' ? 1 : 0);
                prefixY[i][j] = prefixY[i - 1][j] + prefixY[i][j - 1] - prefixY[i - 1][j - 1] + (grid[i - 1][j - 1] == 'Y' ? 1 : 0);
            }
        }

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                for (int k = i; k <= rows; k++) {
                    for (int l = j; l <= cols; l++) {
                        int xCount = prefixX[k][l] - prefixX[i - 1][l] - prefixX[k][j - 1] + prefixX[i - 1][j - 1];
                        int yCount = prefixY[k][l] - prefixY[i - 1][l] - prefixY[k][j - 1] + prefixY[i - 1][j - 1];

                        if (xCount == yCount && xCount > 0) {
                            count++;
                        }
                    }
                }
            }
        }

        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3212 (Count Submatrices With Equal Frequency of X and Y)?

This page provides optimized solutions for LeetCode problem 3212 (Count Submatrices With Equal Frequency of X and Y) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3212 (Count Submatrices With Equal Frequency of X and Y)?

The time complexity for LeetCode 3212 (Count Submatrices With Equal Frequency of X and Y) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3212 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3212 in Java, C++, or Python.

Back to LeetCode Solutions