#2133
Easy Algorithms Check if every row and column contains all numbers
Array Hash Table Matrix
53.9% acceptance
Feb 25, 2026
1078
58
An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).
Given an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.
Solution
Rust
Time O(n²)
Space O(n)
impl Solution {
pub fn check_valid(matrix: Vec<Vec<i32>>) -> bool {
let n = matrix.len();
for i in 0..n {
let mut row = vec![false; n + 1];
let mut col = vec![false; n + 1];
for j in 0..n {
let r = matrix[i][j] as usize;
let c = matrix[j][i] as usize;
if row[r] || col[c] {
return false;
}
row[r] = true;
col[c] = true;
}
}
true
}
}