Lonely Pixel II

medium array hash map matrix

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.

Inputpicture = [["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
Output6
Per column count, then group rows by string key.

def 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;
}
Time: O(R·C) Space: O(R·C)