Valid Sudoku - aloalgo

Valid Sudoku

Easy

You are given a 9x9 2D array of integers, which represents a solved Sudoku board. Each cell board[i][j] within this array contains a single digit from 1 to 9.

Your task is to determine if this solved Sudoku board is indeed valid according to the classic Sudoku rules.

Remember that a Sudoku board is valid if:

  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 nine 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Return true if the Sudoku board is valid, false otherwise.

Example 1

Input
[
  [1, 2, 3, 4, 5, 6, 7, 8, 9],
  [4, 5, 6, 7, 8, 9, 1, 2, 3],
  [7, 8, 9, 1, 2, 3, 4, 5, 6],
  [2, 3, 4, 5, 6, 7, 8, 9, 1],
  [5, 6, 7, 8, 9, 1, 2, 3, 4],
  [8, 9, 1, 2, 3, 4, 5, 6, 7],
  [3, 4, 5, 6, 7, 8, 9, 1, 2],
  [6, 7, 8, 9, 1, 2, 3, 4, 5],
  [9, 1, 2, 3, 4, 5, 6, 7, 8]
]
Output
True
Explanation:

This board is a perfectly solved and valid Sudoku board. All rows, columns, and 3x3 subgrids contain digits 1-9 exactly once.

Example 2

Input
[
  [1, 2, 3, 4, 5, 6, 7, 8, 9],
  [4, 5, 6, 7, 8, 9, 1, 2, 3],
  [2, 3, 4, 5, 6, 7, 8, 9, 1],
  [7, 8, 9, 1, 2, 3, 4, 5, 6],
  [5, 6, 7, 8, 9, 1, 2, 3, 4],
  [8, 9, 1, 2, 3, 4, 5, 6, 7],
  [3, 4, 5, 6, 7, 8, 9, 1, 2],
  [6, 7, 8, 9, 1, 2, 3, 4, 5],
  [9, 1, 2, 3, 4, 5, 6, 7, 8]
]
Output
False
Explanation:

This board is invalid because 2 appears in the 3x3 sub-box starting at (0,0) twice, violating the Sudoku rule that each number must appear exactly once per sub-box.

Example 3

Input
[
  [1, 2, 3, 4, 5, 6, 7, 8, 9],
  [4, 5, 6, 7, 8, 9, 1, 2, 3],
  [2, 3, 4, 5, 6, 7, 8, 9, 1],
  [7, 8, 9, 1, 2, 3, 4, 5, 6],
  [5, 6, 7, 8, 9, 1, 2, 3, 4],
  [8, 9, 1, 2, 3, 4, 5, 6, 7],
  [3, 4, 5, 6, 7, 8, 9, 1, 2],
  [6, 7, 8, 9, 1, 2, 3, 4, 5],
  [1, 2, 3, 4, 5, 6, 7, 8, 9]
]
Output
False
Explanation:

This board is invalid because 1 appears twice in the first column, violating the Sudoku rule that each number must appear exactly once per column.

Loading...
Input
[
  [1, 2, 3, 4, 5, 6, 7, 8, 9],
  [4, 5, 6, 7, 8, 9, 1, 2, 3],
  [7, 8, 9, 1, 2, 3, 4, 5, 6],
  [2, 3, 4, 5, 6, 7, 8, 9, 1],
  [5, 6, 7, 8, 9, 1, 2, 3, 4],
  [8, 9, 1, 2, 3, 4, 5, 6, 7],
  [3, 4, 5, 6, 7, 8, 9, 1, 2],
  [6, 7, 8, 9, 1, 2, 3, 4, 5],
  [9, 1, 2, 3, 4, 5, 6, 7, 8]
]
Output
True

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!