aboutsummaryrefslogtreecommitdiff
path: root/check_every_row_col_contain_num.py
blob: 32d19a0f3c65e7257c7b197251ed35f8015c984e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 2133. Check if Every Row and Column Contains All Numbers

"""
an 'n x n' matrix is valid if every row and every column contains all the
integers from 1 to 'n'. given an 'n x n' matrix, return true if the matrix is
valid, otherwise return false.
"""


class Solution(object):
    def checkValid(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: bool
        """
        n = len(matrix)
        for i in range(n):
            row, col, mask = 1, 1, 1
            for j in range(n):
                row ^= 1 << matrix[i][j]
                col ^= 1 << matrix[j][i]
                mask |= 1 << j + 1
            if row ^ mask or col ^ mask:
                return False
        return True


if __name__ == "__main__":
    obj = Solution()
    print(obj.checkValid(matrix=[[1, 2, 3], [3, 1, 2], [2, 3, 1]]))
    print(obj.checkValid(matrix=[[1, 1, 1], [1, 2, 3], [1, 2, 3]]))