Lonely Pixel I - Problem

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

A black lonely pixel is a character 'B' that is located at a specific position where the same row and same column don't have any other black pixels.

Input & Output

Example 1 — Basic Case
$ Input: picture = [["W","W","B"],["W","B","W"],["W","W","B"]]
Output: 1
💡 Note: Only the black pixel at (1,1) is lonely. Row 1 has 1 black pixel, column 1 has 1 black pixel. The other black pixels have companions in their rows or columns.
Example 2 — No Lonely Pixels
$ Input: picture = [["B","B"],["B","W"]]
Output: 0
💡 Note: No lonely pixels exist. The black pixel at (0,0) shares row 0 with (0,1), pixel at (0,1) shares both row 0 and column 1, and pixel at (1,0) shares column 0.
Example 3 — Single Black Pixel
$ Input: picture = [["W","W"],["W","B"]]
Output: 1
💡 Note: The single black pixel at (1,1) is lonely since it's the only black pixel in both row 1 and column 1.

Constraints

  • m == picture.length
  • n == picture[i].length
  • 1 ≤ m, n ≤ 500
  • picture[i][j] is either 'W' or 'B'

Visualization

Tap to expand
Lonely Pixel I: Find Isolated Black PixelsInput MatrixWWBWBWWWBAnalysisB at (0,2): Row 0 has 1 B, Col 2 has 2 B ❌B at (1,1): Row 1 has 1 B, Col 1 has 1 B ✓B at (2,2): Row 2 has 1 B, Col 2 has 2 B ❌Output11 lonely pixelKey Rule: Lonely = Only B in BOTH row AND column
Understanding the Visualization
1
Input Matrix
Grid with 'B' (black) and 'W' (white) pixels
2
Check Loneliness
For each 'B', verify no other 'B' in same row/column
3
Count Result
Number of lonely black pixels found
Key Takeaway
🎯 Key Insight: A pixel is lonely only if it's the sole black pixel in both its row and column
Asked in
Google 15 Facebook 12 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
245 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