From 77d6e1dd41c644ed28fb61a3f19614965b43908c Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Fri, 12 Sep 2025 22:36:22 -0600 Subject: 2133. check if every row and column contains all numbers --- check_every_row_col_contain_num.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 check_every_row_col_contain_num.py (limited to 'check_every_row_col_contain_num.py') diff --git a/check_every_row_col_contain_num.py b/check_every_row_col_contain_num.py new file mode 100644 index 0000000..32d19a0 --- /dev/null +++ b/check_every_row_col_contain_num.py @@ -0,0 +1,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]])) -- cgit v1.2.3