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

# Lonely Pixel II Python Solution with Tests

> Tested Python solution for LeetCode 533 with 36 pytest cases. Generate a practice environment with lcpy.

LeetCode 533, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/lonely-pixel-ii/description/).

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

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

## Problem

Given an `m x n` `picture` consisting of black `'B'` and white `'W'` pixels and an integer `target`, return the number of **black** lonely pixels.

A black lonely pixel is a character `'B'` located at a specific position `(r, c)` where:

* Row `r` and column `c` both contain exactly `target` black pixels.
* For all rows that have a black pixel at column `c`, they should be exactly the same as row `r`.

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0533.Lonely%20Pixel%20II/images/pixel2-1-grid.jpg)

```
Input: picture = [["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","W","B","W","B","W"]], target = 3
Output: 6
Explanation: All the green 'B's are the black pixels we need (all 'B's at column 1 and 3).
Take 'B' at row r = 0 and column c = 1 as an example:
 - Rule 1, row r = 0 and column c = 1 both have exactly target = 3 black pixels.
 - Rule 2, the rows that have a black pixel at column c = 1 are row 0, row 1 and row 2. They are exactly the same as row r = 0.
```

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0533.Lonely%20Pixel%20II/images/pixel2-2-grid.jpg)

```
Input: picture = [["W","W","B"],["W","W","B"],["W","W","B"]], target = 1
Output: 0
```

### Constraints

* `m == picture.length`
* `n == picture[i].length`
* `1 <= m, n <= 200`
* `picture[i][j]` is `'W'` or `'B'`.
* `1 <= target <= min(m, n)`

## Solution

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

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


class Solution:
    # Time: O(m * n^2)
    # Space: O(m * n)
    def find_black_pixel(self, picture: list[list[str]], target: int) -> int:
        row_counts = [row.count("B") for row in picture]
        cols: dict[int, list[int]] = defaultdict(list)
        for i, row in enumerate(picture):
            for j, pixel in enumerate(row):
                if pixel == "B":
                    cols[j].append(i)

        result = 0
        for rows in cols.values():
            if row_counts[rows[0]] != target or len(rows) != target:
                continue
            if all(picture[r] == picture[rows[0]] for r in rows):
                result += target
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n^2) | O(m \* n) |

## Tags


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