Skip to main content
impol.dev

Count Submatrices With Equal Frequency of X and Y

Medium

Provided a 2D character matrix grid, where grid[i][j] is either 'X', 'Y', or '.', produce the number of submatrices that contain:

  • grid[0][0]

  • an equal frequency of 'X' and 'Y'.

  • at least one 'X'.

Example 1

Original illustrative diagram 1 for Count Submatrices With Equal Frequency of X and Y

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 '.'.

Did this problem appear in one of your interviews?

Responses stay private.

Acceptance rate: 100%