Valid Sudoku
Determine if a 9x9 Sudoku board is valid. A valid board must have no duplicate numbers in any row, column, or 3x3 sub-box.
Write a function that checks if a Sudoku board is valid. A valid Sudoku board must satisfy three conditions:
- Each row must contain digits 1-9 without repetition
- Each column must contain digits 1-9 without repetition
- Each of the 9 3x3 sub-boxes must contain digits 1-9 without repetition
Empty cells are represented by '.'.
How to Solve
Use sets or hash maps to track seen digits in rows, columns, and sub-boxes. For each cell, check if the digit has already appeared in its row, column, or 3x3 box. The key insight is calculating which sub-box a cell belongs to: boxIndex = Math.floor(row / 3) * 3 + Math.floor(col / 3).
Code
Test Cases
Input 1
Expected Output: true
Input 2
Expected Output: false (duplicate 8 in first column)
Input 3
Expected Output: true (empty board is valid)
Complexity
Time Complexity: O(1) - We always iterate through a fixed 9x9 grid (81 cells), so it's constant time.
Space Complexity: O(1) - We use sets/maps with at most 9 entries each, which is constant space.