Lonely Pixel II
Problem
Count black pixels (r, c) such that the column has exactly target B's and all rows containing a B in column c are identical.
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 = 36def find_black_pixel(picture, target):
R, C = len(picture), len(picture[0])
col = [0] * C; row_keys = ["".join(r) for r in picture]
from collections import Counter
rc = Counter(row_keys)
for j in range(C):
for i in range(R):
if picture[i][j] == "B": col[j] += 1
ans = 0
for j in range(C):
if col[j] != target: continue
# take first row that has B in col j as canonical
key = None
for i in range(R):
if picture[i][j] == "B": key = row_keys[i]; break
if rc[key] == target and key.count("B") >= target:
ans += target
return ans
function findBlackPixel(p, target) {
const R = p.length, C = p[0].length;
const rowKeys = p.map(r => r.join(""));
const counts = new Map();
for (const k of rowKeys) counts.set(k, (counts.get(k) || 0) + 1);
const col = new Array(C).fill(0);
for (let i = 0; i < R; i++) for (let j = 0; j < C; j++) if (p[i][j] === "B") col[j]++;
let ans = 0;
for (let j = 0; j < C; j++) {
if (col[j] !== target) continue;
let key = null;
for (let i = 0; i < R; i++) if (p[i][j] === "B") { key = rowKeys[i]; break; }
if (counts.get(key) === target) ans += target;
}
return ans;
}
class Solution {
public int findBlackPixel(char[][] p, int target) {
int R = p.length, C = p[0].length;
String[] rk = new String[R];
for (int i = 0; i < R; i++) rk[i] = new String(p[i]);
Map counts = new HashMap<>();
for (String s : rk) counts.merge(s, 1, Integer::sum);
int[] col = new int[C];
for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) if (p[i][j] == 'B') col[j]++;
int ans = 0;
for (int j = 0; j < C; j++) {
if (col[j] != target) continue;
String key = null;
for (int i = 0; i < R; i++) if (p[i][j] == 'B') { key = rk[i]; break; }
if (counts.getOrDefault(key, 0) == target) ans += target;
}
return ans;
}
}
int findBlackPixel(vector>& p, int target) {
int R = p.size(), C = p[0].size();
vector rk(R);
for (int i = 0; i < R; i++) rk[i] = string(p[i].begin(), p[i].end());
unordered_map counts;
for (auto& s : rk) counts[s]++;
vector col(C);
for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) if (p[i][j] == 'B') col[j]++;
int ans = 0;
for (int j = 0; j < C; j++) {
if (col[j] != target) continue;
string key;
for (int i = 0; i < R; i++) if (p[i][j] == 'B') { key = rk[i]; break; }
if (counts[key] == target) ans += target;
}
return ans;
}
Explanation
This time a black pixel counts only if its column has exactly target black pixels, and every row that has a "B" in that column looks identical. The trick is to represent each row as a single string key so identical rows are easy to spot.
We turn each row into a string (like "WBWBBW") and use a counter map to record how many times each exact row pattern appears.
We also count black pixels per column. Then for each column whose count equals target, we grab the pattern of the first row that has a "B" there as the canonical row.
If that pattern appears exactly target times in the whole grid, it means all the rows lighting up this column are the same row pattern, so those target pixels qualify and we add target to the answer.
Example: three identical rows "WBWBBW" share columns 1, 3, 4. Each of those columns has 3 "B"s (= target) and the row pattern repeats 3 times, contributing 3 + 3 = 6.