Count Submatrices With Equal Frequency of X and Y - Problem

You're given a 2D character matrix grid where each cell contains either 'X', 'Y', or '.'. Your task is to find the number of submatrices that satisfy all of the following conditions:

  • The submatrix must include the top-left corner grid[0][0]
  • The submatrix contains an equal frequency of 'X' and 'Y' characters
  • The submatrix contains at least one 'X' character

A submatrix is defined by its bottom-right corner (i, j), where the submatrix includes all cells from (0, 0) to (i, j) inclusive.

Example: If grid[0][0] = 'X' and grid[0][1] = 'Y', then the submatrix ending at (0, 1) has 1 'X' and 1 'Y', satisfying our conditions.

Input & Output

example_1.py β€” Basic balanced submatrix
$ Input: grid = [["X","Y"],["Y","X"]]
β€Ί Output: 2
πŸ’‘ Note: Two valid submatrices: (0,0)β†’(0,1) with 1 X and 1 Y, and (0,0)β†’(1,1) with 2 X's and 2 Y's
example_2.py β€” Single cell case
$ Input: grid = [["X"]]
β€Ί Output: 0
πŸ’‘ Note: Only one submatrix (0,0)β†’(0,0) with 1 X and 0 Y's - not balanced, so result is 0
example_3.py β€” Mixed with dots
$ Input: grid = [["X",".","Y"],[".","Y","X"]]
β€Ί Output: 1
πŸ’‘ Note: Only the full submatrix (0,0)β†’(1,2) has balanced counts: 2 X's and 2 Y's

Visualization

Tap to expand
πŸ—ΊοΈ Treasure Map AnalysisTreasure MapπŸ’Žβš‘πŸ•³οΈβš‘πŸ’ŽπŸ•³οΈπŸ•³οΈπŸ•³οΈπŸ’ŽBalanced Region: 2πŸ’Ž = 2⚑Smart Counting System:πŸ“Š Prefix Count TablesTreasures (πŸ’Ž): [1,1,1] [1,2,2] [1,2,3]Traps (⚑): [0,1,1] [1,2,2]⚑ O(1) Queries!Expedition Results:βœ… Region (0,0)β†’(0,1)1πŸ’Ž = 1⚑ βœ“βœ… Region (0,0)β†’(1,1)2πŸ’Ž = 2⚑ βœ“βŒ Region (0,0)β†’(0,0)1πŸ’Ž β‰  0⚑ βœ—βŒ Region (0,0)β†’(2,2)3πŸ’Ž β‰  2⚑ βœ—πŸŽ― Expedition StrategyUse prefix sums to instantly calculate treasure-trap balance in any regionTime: O(mΓ—n) | Space: O(mΓ—n) | Perfect for treasure hunting efficiency!
Understanding the Visualization
1
Build counting tables
Create cumulative count tables for treasures (X) and traps (Y)
2
Evaluate regions
For each possible rectangular region starting from origin, check balance
3
Apply criteria
Count regions where treasures equal traps and at least one treasure exists
4
Report findings
Return the total count of viable expedition regions
Key Takeaway
🎯 Key Insight: Prefix sums transform an O(mΒ²Γ—nΒ²) brute force solution into an elegant O(mΓ—n) algorithm by precomputing cumulative counts, enabling instant submatrix queries.

Time & Space Complexity

Time Complexity
⏱️
O(m Γ— n)

O(mΓ—n) to build prefix sums + O(mΓ—n) to check all endpoints

n
2n
βœ“ Linear Growth
Space Complexity
O(m Γ— n)

Two prefix sum arrays of size mΓ—n for X and Y counts

n
2n
⚑ Linearithmic Space

Constraints

  • 1 ≀ grid.length, grid[i].length ≀ 100
  • grid[i][j] is either 'X', 'Y', or '.'
  • The submatrix must contain grid[0][0]
  • At least one 'X' must be present in valid submatrices
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
29.2K Views
Medium Frequency
~18 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen