531. Lonely Pixel I

Given a picture consisting of black and white pixels, find the number ofblacklonely pixels.

The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively.

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

Example:

Input:

[['W', 'W', 'B'],
 ['W', 'B', 'W'],
 ['B', 'W', 'W']]


Output:
 3

Explanation:
 All the three 'B's are black lonely pixels.
class Solution {
    public int findLonelyPixel(char[][] picture) {
        if (picture == null || picture.length == 0 || picture[0].length == 0) return 0;
        int n = picture.length, m = picture[0].length;
        int[] colHash = new int[m];

        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (picture[i][j] == 'B') colHash[j]++;
            }
        }

        int ans = 0;
        for (int i = 0; i < n; i++){
            int count = 0, col = 0;
            for (int j = 0; j < m; j++){
                if (picture[i][j] == 'B'){
                    count++;
                    col = j;
                }
            }
            if (count == 1 && colHash[col] == 1) ans++;
        }

        return ans;
    }
}

results for ""

    No results matching ""