LeetCode solutions

2254. Check if Every Row and Column Contains All Numbers

My accepted Python solution to LeetCode problem 2254, Check if Every Row and Column Contains All Numbers, running in 23ms.

  • Difficulty: Easy
  • Python
  • Runtime 23ms
  • Memory 18.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 23ms, memory 18.4MB, accepted 2025-12-29.

python
class Solution:
    def checkValid(self, matrix: List[List[int]]) -> bool:
        n = len(matrix)
        expected = set(range(1, n + 1))
        
        # Check rows
        for row in matrix:
            if set(row) != expected:
                return False
        
        # Check columns
        for col in range(n):
            column = set(matrix[row][col] for row in range(n))
            if column != expected:
                return False
        
        return True

Source