> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Image Overlap Python Solution with Tests

> Tested Python solution for LeetCode 835 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 835, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/image-overlap/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 835   # by problem number
lcpy gen -s image_overlap   # by problem name
```

## Problem

You are given two images, `img1` and `img2`, represented as binary, square matrices of size `n x n`. A binary matrix has only `0`s and `1`s as values.

We **translate** one image however we choose by sliding all the `1` bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the **overlap** by counting the number of positions that have a `1` in **both** images.

Note also that a translation does **not** include any kind of rotation. Any `1` bits that are translated outside of the matrix borders are erased.

Return *the largest possible overlap*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/09/09/overlap1.jpg)

```
Input: img1 = [[1,1,0],[0,1,0],[0,1,0]], img2 = [[0,0,0],[0,1,1],[0,0,1]]
Output: 3
Explanation: We translate img1 to right by 1 unit and down by 1 unit.
```

![Step 1](https://assets.leetcode.com/uploads/2020/09/09/overlap_step1.jpg)

The number of positions that have a 1 in both images is 3 (shown in red).

![Step 2](https://assets.leetcode.com/uploads/2020/09/09/overlap_step2.jpg)

```
Input: img1 = [[1]], img2 = [[1]]
Output: 1
```

```
Input: img1 = [[0]], img2 = [[0]]
Output: 0
```

### Constraints

* n == img1.length == img1\[i].length
* n == img2.length == img2\[i].length
* 1 \<= n \<= 30
* img1\[i]\[j] is either 0 or 1.
* img2\[i]\[j] is either 0 or 1.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/image_overlap/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/image_overlap/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n^4) where n is the image size (pairs of 1 bits across both images)
    # Space: O(n^2) for the shift counter
    def largest_overlap(self, img1: list[list[int]], img2: list[list[int]]) -> int:
        ones1 = [(i, j) for i, row in enumerate(img1) for j, val in enumerate(row) if val]
        ones2 = [(i, j) for i, row in enumerate(img2) for j, val in enumerate(row) if val]
        shifts: Counter[tuple[int, int]] = Counter()
        best = 0
        for i1, j1 in ones1:
            for i2, j2 in ones2:
                shift = (i2 - i1, j2 - j1)
                shifts[shift] += 1
                best = max(best, shifts[shift])
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n^4) where n is the image size (pairs of 1 bits across both images) | O(n^2) for the shift counter |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.