Count Cells in Overlapping Horizontal and Vertical Substrings - Problem

You are given an m ร— n matrix grid consisting of characters and a pattern string. Your task is to find cells that are part of both a horizontal and vertical occurrence of the pattern.

Horizontal Reading: Read characters from left to right. When you reach the end of a row, wrap to the first column of the next row and continue. You cannot wrap from the last row back to the first.

Vertical Reading: Read characters from top to bottom. When you reach the bottom of a column, wrap to the first row of the next column and continue. You cannot wrap from the last column back to the first.

Goal: Count cells that belong to at least one horizontal substring AND at least one vertical substring, where both substrings match the given pattern.

Example: If pattern = "AB" and we find "AB" horizontally starting at (0,0) and "AB" vertically starting at (0,0), then cell (0,0) contributes to our count since it's part of both occurrences.

Input & Output

example_1.py โ€” Basic Example
$ Input: grid = [["A","B","C"],["D","A","B"]], pattern = "AB"
โ€บ Output: 2
๐Ÿ’ก Note: Horizontal: "AB" found at positions (0,0)-(0,1). Vertical: "AB" found at positions (1,1)-(0,2) with wrapping. Cell (0,1) is part of both matches, contributing to count.
example_2.py โ€” No Overlaps
$ Input: grid = [["A","B"],["C","D"]], pattern = "AC"
โ€บ Output: 0
๐Ÿ’ก Note: "AC" found horizontally starting at (0,0) with wrapping to (1,0). "AC" not found vertically. No cells are part of both horizontal and vertical matches.
example_3.py โ€” Multiple Overlaps
$ Input: grid = [["X","Y","X"],["Y","X","Y"]], pattern = "XY"
โ€บ Output: 3
๐Ÿ’ก Note: Multiple "XY" patterns found both horizontally and vertically. Several cells participate in both types of matches, resulting in count of 3.

Visualization

Tap to expand
Matrix Pattern Matching VisualizationABCDABPattern: "AB"โ–  Horizontal Match Cellsโ–  Vertical Match Cellsโ†’ Horizontal Reading (with wrap)โ†“ Vertical Reading (with wrap)Horizontal String: "ABCDAB"Vertical String: "ADACBB"Overlapping Cells: (0,0), (0,1), (1,1), (1,2)Result: 4 cells
Understanding the Visualization
1
Understand Wrapping
Horizontal reading wraps from end of row to start of next row. Vertical reading wraps from bottom of column to top of next column.
2
Find Horizontal Matches
Search for the pattern in the horizontally linearized string and track which matrix cells are involved.
3
Find Vertical Matches
Search for the pattern in the vertically linearized string and track which matrix cells are involved.
4
Count Overlaps
Find cells that appear in both horizontal and vertical match sets - these are our answer.
Key Takeaway
๐ŸŽฏ Key Insight: By linearizing the matrix reading patterns and using efficient string matching algorithms, we can solve this problem optimally in O(mร—n + p) time rather than the naive O(mร—nร—p) approach.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m*n + p)

KMP preprocessing is O(p), searching in linearized strings is O(m*n), total is O(m*n + p)

n
2n
โœ“ Linear Growth
Space Complexity
O(m*n + p)

Space for linearized strings O(m*n) plus KMP failure function O(p)

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค m, n โ‰ค 100 (matrix dimensions)
  • 1 โ‰ค pattern.length โ‰ค m ร— n
  • grid[i][j] and pattern consist of lowercase English letters only
  • Pattern length does not exceed total matrix size
  • No empty inputs: grid, pattern are non-empty
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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