aboutsummaryrefslogtreecommitdiff
path: root/check_every_row_col_contain_num.py
diff options
context:
space:
mode:
Diffstat (limited to 'check_every_row_col_contain_num.py')
-rw-r--r--check_every_row_col_contain_num.py31
1 files changed, 31 insertions, 0 deletions
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]]))