Check if Every Row and Column Contains All Numbers Solution
Check if Every Row and Column Contains All Numbers solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Check if Every Row and Column Contains All Numbers is an easy coding problem on arrays, hash table and matrix, asked in Amazon, Google and Indeed interviews.
- Difficulty: Easy
- Topics: Arrays, Hash Table, Matrix
- Asked at: Amazon, Google, Indeed
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
An n x n matrix is valid when every row and every column contains each of the integers 1 to n (so each exactly once).
Given an n x n matrix whose entries are all between 1 and n, return true if it is valid.
Example 1
Input: matrix = [[3,1,2],[1,2,3],[2,3,1]]
Output: true
Example 2
Input: matrix = [[1,2,3],[2,3,1],[1,3,2]]
Output: false
Explanation: Every row is fine, but column 0 is [1,2,1]: it repeats 1 and misses 3.
Example 3
Input: matrix = [[1]]
Output: true
Constraints
n == matrix.length == matrix[i].length1 <= n <= 1001 <= matrix[i][j] <= n
Hints for Check if Every Row and Column Contains All Numbers
Hint 1
A line of
nvalues drawn from 1..n contains every number exactly when it has no repeated value.Hint 2
So the task reduces to: no row and no column has a duplicate.
Hint 3
Check row
iand columnitogether with two 'seen' arrays of sizen + 1.
How to solve Check if Every Row and Column Contains All Numbers
Since every entry is already in 1..n, a row or column of n entries covers all of 1..n if and only if it has no duplicates — so duplicate detection per line is enough.
Approach
- For each
ifrom 0 ton - 1, clear two boolean arraysrowandcolof sizen + 1. - For each
j, look ata = matrix[i][j]andb = matrix[j][i]; ifrow[a]orcol[b]is already set, returnfalse; otherwise set both. - Return
truewhen every row and column passes.
Why it works
By the pigeonhole principle, n values from a set of size n with no repetition must use every value once. Checking row i and column i in the same loop visits each line exactly once.
Complexity
- Time —
O(n^2) - Space —
O(n)
Pitfalls
- Checking only rows (or only sums) is not enough:
[[1,2],[1,2]]has perfect rows but bad columns. - Equal row sums prove nothing —
[2,2,2]sums like[1,2,3]. - Reset the seen arrays for each new row and column.
Reference solution
Python
from typing import List
def checkValid(matrix: List[List[int]]) -> bool:
n = len(matrix)
for i in range(n):
if len(set(matrix[i])) != n:
return False
if len(set(matrix[j][i] for j in range(n))) != n:
return False
return TrueJava
public static boolean checkValid(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
boolean[] row = new boolean[n + 1], col = new boolean[n + 1];
for (int j = 0; j < n; j++) {
int a = matrix[i][j], b = matrix[j][i];
if (row[a] || col[b]) return false;
row[a] = true;
col[b] = true;
}
}
return true;
}C++
bool checkValid(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++) {
vector<bool> row(n + 1, false), col(n + 1, false);
for (int j = 0; j < n; j++) {
int a = matrix[i][j], b = matrix[j][i];
if (row[a] || col[b]) return false;
row[a] = true;
col[b] = true;
}
}
return true;
}JavaScript
var checkValid = function(matrix) {
var n = matrix.length;
for (var i = 0; i < n; i++) {
var row = new Array(n + 1).fill(false), col = new Array(n + 1).fill(false);
for (var j = 0; j < n; j++) {
var a = matrix[i][j], b = matrix[j][i];
if (row[a] || col[b]) return false;
row[a] = true;
col[b] = true;
}
}
return true;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Hashing: Hash Maps and Hash Sets