Skip to content

Practice Count Cells in Overlapping Horizontal and Vertical Substrings

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3529
LeetCode Problem

Count Cells in Overlapping Horizontal and Vertical Substrings

Problem Statement

Read grid horizontally in row-major order, continuing from the end of a row to the start of the next, and vertically in column-major order, continuing from the bottom of a column to the top of the next. Neither reading wraps beyond the final row or column. Count the cells covered by at least one occurrence of pattern in each reading direction, and return that count.

Source: Count Cells in Overlapping Horizontal and Vertical Substrings on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [["a","a","c","c"],["b","b","b","c"],["a","a","b","a"],["c","a","a","c"],["a","a","b","a"]], pattern = "abaca"
Output
1

# Example 2

Input
grid = [["c","a","a","a"],["a","a","b","a"],["b","b","a","a"],["a","a","b","a"]], pattern = "aba"
Output
4

# Example 3

Input
grid = [["a"]], pattern = "a"
Output
1

Constraints

m == grid.length
n == grid[i].length
1 <= m, n <= 1000
1 <= m * n <= 10^5
1 <= pattern.length <= m * n
grid and pattern consist of only lowercase English letters.

Related LeetCode Problems

Practice more problems similar to Count Cells in Overlapping Horizontal and Vertical Substrings.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.