Lonely Pixel II - 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' that 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.

Input & Output

Example 1 — Basic Case
$ Input: picture = [["W","B","W"],["B","W","B"],["W","B","W"]], target = 2
Output: 2
💡 Note: Row 1 has 2 black pixels and columns 0,2 each have 2 black pixels. All rows with black pixels in columns 0,2 are identical to row 1.
Example 2 — No Lonely Pixels
$ Input: picture = [["W","B"],["B","W"]], target = 1
Output: 0
💡 Note: Each black pixel's row and column have exactly 1 black pixel, but rows 0 and 1 are different, violating the identical row condition.
Example 3 — All Qualify
$ Input: picture = [["B","B"],["B","B"]], target = 2
Output: 4
💡 Note: All rows are identical with 2 black pixels each, and all columns have 2 black pixels each.

Constraints

  • 1 ≤ m, n ≤ 200
  • 1 ≤ target ≤ min(m, n)
  • picture[i][j] is either 'W' or 'B'

Visualization

Tap to expand
Lonely Pixel II: Three Conditions for Lonely Black PixelsBBBBtarget = 2Three Conditions1. Row has exactly 2 black pixels2. Column has exactly 2 black pixels3. All rows with B in same columnare identicalResult2 lonely pixelsRow 1: BWB pattern has 2 blacks, columns 0,2 each have 2 blacksOnly row 1 has black pixels in columns 0,2, so condition 3 satisfiedBoth black pixels in row 1 are lonely pixels
Understanding the Visualization
1
Input Matrix
Black and white pixel matrix with target count
2
Check Conditions
Row count, column count, and identical row requirements
3
Count Result
Number of black pixels satisfying all conditions
Key Takeaway
🎯 Key Insight: Group identical rows to efficiently validate the 'same row' condition for all pixels in a column
Asked in
Google 15 Facebook 8 Microsoft 5
12.4K Views
Medium Frequency
~25 min Avg. Time
485 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