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

# 36. Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

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

A partially filled sudoku which is valid.

Note: A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

## Solution <a href="#solution" id="solution"></a>

```java
public class Solution {
    public boolean isValidSudoku(char[][] board) {

     for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {

                if (board[i][j] != '.' && !isValid(board, i, j))
                    return false;

            }
        }

        return true;

    }

    public static boolean isValid(char[][] board, int row, int column) {
        if (row < 0 || row >= 9 || column < 0 || column >= 9) return false;

        char val = board[row][column];
        for (int i = 0; i < 9; i++) {
            if (board[row][i] == val && i != column) return false;
        }

        for (int i = 0; i < 9; i++) {
            if (board[i][column] == val && i != row) return false;
        }

        // 3 * 3
        int startX = ((int) row / 3) * 3;
        int startY = ((int) column / 3) * 3;
        for (int i = startX; i < startX + 3; i++) {
            for (int j = startY; j < startY + 3; j++) {
                if (board[i][j] == val && (i != row || j != column)) {
                    return false;
                }
            }
        }

        return true;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://anand-aryan.gitbook.io/leetcode/36.-valid-sudoku.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.
