Problem statement

https://leetcode.com/problems/lonely-pixel-i/

Solution

Just compute number of pixels in each row and in each column. Then traverse picture once again and check if we have black pixel and if number of pixels in row and column is equal to 1.

Complexity

Time complexity is O(mn), space complexity is O(m+n).

Code

class Solution:
    def findLonelyPixel(self, A):
        m, n, ans = len(A), len(A[0]), 0

        rows, cols = [0]*m, [0]*n

        for r, c in product(range(m), range(n)):
            rows[r] += (A[r][c] == "B")
            cols[c] += (A[r][c] == "B")

        for r, c in product(range(m), range(n)):
            ans += (rows[r] == 1) and (cols[c] == 1) and (A[r][c] == "B")

        return ans