# 2133. Check if Every Row and Column Contains All Numbers

##

## use set to check row, then col

```java
class Solution {
    public boolean checkValid(int[][] matrix) {
        int n = matrix.length;

        Set<Integer> set = new HashSet<>();
        for (int i = 0 ; i < n; i++) {
            for (int j = 0 ; j < n; j++) {
                // if there is a number existed, it's wrong!
                if (set.contains(matrix[i][j])) {
                    return false;
                }
                set.add(matrix[i][j]);
            }
            set = new HashSet<>();
        }
        
        set = new HashSet<>();
        for (int j = 0 ; j < n; j++) {
            for (int i = 0 ; i < n; i++) {
                // if there is a number existed, it's wrong!
                if (set.contains(matrix[i][j])) {
                    return false;
                }
                set.add(matrix[i][j]);
            }
            set = new HashSet<>();
        }
        return true;
    }
}
```

## use Xor

![](https://4272748102-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LekNH5IywF8mjBxFcnu%2Fuploads%2FqIv9uVLsd09XVpb3CL0S%2Fimage.png?alt=media\&token=c7f03f0b-bc8b-4b31-bbd1-52d256e217ae)

T: O(n^2)

S: O(1)

```java
class Solution {
    public boolean checkValid(int[][] matrix) {
        int n = matrix.length;
        
        for (int i = 0 ; i < n; i++) {
            int rowXor = 0; // 逐 row 檢查
            int colXor = 0; // 逐 col 檢查
            for (int j = 0 ; j < n; j++) {
                rowXor ^= matrix[i][j]^(j+1);
                colXor ^= matrix[j][i]^(j+1);
            }
            if (rowXor != 0 || colXor != 0) {
                return false;
            }
        }
        
        
        return true;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/weekly-contest/weekly-contest-275/2133.-check-if-every-row-and-column-contains-all-numbers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
