Hone logo
Hone
Problems

Valid Sudoku Checker

A Sudoku puzzle is a 9x9 grid filled with digits from 1 to 9, such that each row, each column, and each of the nine 3x3 subgrids contains all of the digits from 1 to 9 exactly once. This challenge asks you to write a function that determines if a given 9x9 Sudoku board is valid according to these rules. This is a fundamental problem in many Sudoku solver applications and game validations.

Problem Description

Given a partially filled 9x9 Sudoku board, determine if it is valid. A Sudoku board is valid if and only 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 subgrids of the grid must contain the digits 1-9 without repetition.

The input board will be a 2D array (or equivalent structure) representing the 9x9 grid. Empty cells are typically represented by a specific character or value (e.g., a dot . or zero 0). The problem statement usually specifies what represents an empty cell. For this challenge, assume empty cells are represented by the character . (dot).

Key Requirements:

  • The function should take the 9x9 Sudoku board as input.
  • It should return True if the board is valid according to the rules, and False otherwise.
  • Only the filled cells need to be validated. Empty cells (.) do not violate any rules.

Edge Cases to Consider:

  • A completely empty board (all .): This should be considered valid as no rules are violated.
  • A completely filled, valid board.
  • A board with multiple violations. The function can return False as soon as it detects any violation.

Examples

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
Explanation: The board is valid because each row, column, and 3x3 subgrid contains distinct digits from 1-9 (ignoring empty cells).

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: The first column has two "8"s, violating the rule.

Example 3: (Valid, but requires checking subgrids)

Input:
[
  ["1","2","3",".",".",".",".",".","."],
  ["4","5","6",".",".",".",".",".","."],
  ["7","8","9",".",".",".",".",".","."],
  [".",".",".",".",".",".",".",".","."],
  [".",".",".",".",".",".",".",".","."],
  [".",".",".",".",".",".",".",".","."],
  [".",".",".",".",".",".",".",".","."],
  [".",".",".",".",".",".",".",".","."],
  [".",".",".",".",".",".",".",".","."]
]
Output: True
Explanation: Rows and columns are valid. The top-left 3x3 subgrid contains 1-9 without repetition. The remaining cells are empty, so they don't cause violations.

Constraints

  • board will be a 9x9 2D array (or equivalent structure).
  • Each element of board will be a character from the set {'1', '2', '3', '4', '5', '6', '7', '8', '9', '.'}.
  • The solution should be efficient. A time complexity of O(N^2), where N is the dimension of the board (N=9), is expected.

Notes

  • You will need to check rows, columns, and 3x3 subgrids independently.
  • Consider how you can efficiently track the digits seen in each row, column, and subgrid to detect duplicates. Data structures like sets or frequency maps can be very helpful here.
  • The 3x3 subgrids can be identified by calculating their starting row and column indices. For a cell at (row, col), its 3x3 subgrid can be indexed by (row // 3, col // 3).
Loading editor...
plaintext