> For the complete documentation index, see [llms.txt](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/array/36.-valid-sudoku.md).

# 36. Valid Sudoku

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:

1. Each row must contain the digits `1-9` without repetition.
2. Each column must contain the digits `1-9` without repetition.
3. Each of the 9 `3x3` sub-boxes of the grid must contain the digits `1-9` without repetition.

![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)\
A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character `'.'`.

**Example 1:**

```
Input:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: true
```

**Example 2:**

```
Input:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being 
    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
```

**Note:**

* A Sudoku board (partially filled) could be valid but is not necessarily solvable.
* Only the filled cells need to be validated according to the mentioned rules.
* The given board contain only digits `1-9` and the character `'.'`.
* The given board size is always `9x9`.

```cpp
bool isValidSudoku(vector<vector<char>>& board) { // time: O(n^2); space: O(n^2)
    if (board.empty() || board[0].empty() || board.size() != board[0].size()) return false;
    int n = board.size();
    vector<vector<bool> > row(n, vector<bool>(n, false)), col(n, vector<bool>(n, false)), box(n, vector<bool>(n, false));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (board[i][j] == '.') continue;
            int k = board[i][j] - '1';
            if (row[i][k] || col[j][k] || box[3 * (i / 3) + (j / 3)][k]) return false;
            row[i][k] = col[j][k] = box[3 * (i / 3) + (j / 3)][k] = true;
        }
    }
    return true;
}
```

```cpp
// Optimized Space
bool isValidSudoku(vector<vector<char>>& board) { // time: O(n^2); space: O(n)
    if (board.empty() || board[0].empty() || board.size() != board[0].size()) return false;
    int n = board.size();
    vector<short> row(n, 0), col(n, 0), box(n, 0);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (board[i][j] == '.') continue;
            int mask = 1 << (board[i][j] - '1');
            int k = 3 * (i / 3) + (j / 3);
            if (row[i] & mask || col[j] & mask || box[k] & mask) return false;
            row[i] |= mask;
            col[j] |= mask;
            box[k] |= mask;
        }
    }
    return true;
}
```

{% content-ref url="/pages/-Ldo4Zcn82Jp3LFzjZmC" %}
[37. Sudoku Solver](/practice-of-algorithm-problems/backtracking/37.-sudoku-solver.md)
{% endcontent-ref %}
