549. Lonely Pixel II

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.

Example 1:

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' 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 have 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:

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

Output: 0

Explanation:

  Condition 1 (Target = 1):

  •     Each row has exactly 1 black pixel ('B').
  •     Column 2 also has 3 black pixels, not just 1.

  Condition 2:

  •     The rows with a black pixel in column 2 are not the same (they all contain "W" in the first two columns and "B" in the third).

Since there’s no column with exactly 1 black pixel and the rows don't match, there are no lonely black pixels.

Now Your Turn!

Pick the correct output for the given input

Input : pricture =[["B", "W", "B", "W"],["B", "W", "B", "W"],["W", "W", "B", "W"],["B", "B", "B", "W"]] , target = 2

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int findBlackPixel(vector<vector<char>>& picture, int target) {
// Your code goes here
}
};
Test Case

Input:

Picture
Target