public class Solution {
public int findBlackPixel(char[][] picture, int N) {
if (picture == null || picture.length < 1 || picture[0].length < 1) return 0;
int row = picture.length;
int col = picture[0].length;
int[] cntR = new int[row];
int[] cntC = new int[col];
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < row; i++) {
map.put(String.valueOf(picture[i]), map.getOrDefault(String.valueOf(picture[i]), 0) + 1);
for (int j = 0; j < col; j++) {
if (picture[i][j] == 'B') {
cntR[i]++;
cntC[j]++;
}
}
}
int res = 0;
for (int i = 0; i < row; i++) {
if (cntR[i] != N) continue;
for (int j = 0; j < col; j++) {
if (picture[i][j] == 'B' && cntC[j] == N && map.getOrDefault(String.valueOf(picture[i]), 0) == N) {
res++;
}
}
}
return res;
}
}
public class Solution {
public int findBlackPixel(char[][] picture, int N) {
if (picture.length == 0) return 0;
int m = picture.length;
int n = picture[0].length;
int[] rowCounts = new int[m];
int[] colCounts = new int[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (picture[i][j] == 'B') {
rowCounts[i]++;
colCounts[j]++;
}
}
}
int ret = 0;
for (int j = 0; j < n; j++) {
if (colCounts[j] == N) {
for (int i = 0; i < m; i++) {
if (rowCounts[i] != N) continue;
if (picture[i][j] != 'B') continue;
if (!match(picture, i, j)) {
break;
} else {
ret += N;
break;
}
}
}
}
return ret;
}
private boolean match(char[][] picture, int row, int col) {
for (int i = 0; i < picture.length; i++) {
if (i != row && picture[i][col] == 'B') {
for (int j = 0; j < picture[0].length; j++) {
if (picture[i][j] != picture[row][j]) {
return false;
}
}
}
}
return true;
}
}