LeetCode Challenge: Solve Valid Sudoku in JavaScript
A Step-by-Step Guide to Problem #36
🚀 Problem Description
Given a 9x9 Sudoku board, your task is to validate its current configuration. The board is valid if it adheres to the following rules:
- Row Validation: Each row must contain unique digits from
1-9
. - Column Validation: Each column must contain unique digits from
1-9
. - 3x3 Sub-Box Validation: Each of the nine 3x3 grids must also contain unique digits from
1-9
.
Additional Notes:
- Empty cells are represented by
"."
and are ignored during validation. - You do not need to solve the board; only validate the current configuration.
💡 Examples
Example 1: Valid Board
const board = [
["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"],
];
console.log(isValidSudoku(board)); // Output: true
Example 2: Invalid Board
const board = [
["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"],
];
console.log(isValidSudoku(board)); // Output: false
🏆 JavaScript Solution
To validate the board, we use three key checks:
- Rows: Ensure each row has unique digits.
- Columns: Ensure each column has unique digits.
- Sub-Boxes: Validate all nine 3x3 grids for unique digits.
Implementation
function isValidSudoku(board) {
const rows = Array.from({ length: 9 }, () => new Set());
const cols = Array.from({ length: 9 }, () => new Set());
const boxes = Array.from({ length: 9 }, () => new Set());
for (let r = 0; r < 9; r++) {
for (let c = 0; c < 9; c++) {
const value = board[r][c];
if (value === ".") continue;
const boxIndex = Math.floor(r / 3) * 3 + Math.floor(c / 3);
if (rows[r].has(value) || cols[c].has(value) || boxes[boxIndex].has(value)) {
return false;
}
rows[r].add(value);
cols[c].add(value);
boxes[boxIndex].add(value);
}
}
return true;
}
🔍 How It Works
- Initialization: Create three arrays of
Set
objects for tracking numbers in rows, columns, and sub-boxes. - Iterate through the board:
- Skip empty cells (
"."
). - Compute the box index dynamically:
Math.floor(r / 3) * 3 + Math.floor(c / 3)
. - Check for duplicates in the corresponding row, column, or sub-box. If found, return
false
. - Add the value to the respective row, column, and box sets.
- Skip empty cells (
- Return Result: If all checks pass, return
true
.
📊 Complexity Analysis
Time Complexity: O(81) → O(1) (since the board size is fixed).
Space Complexity: O(27) → O(1) (27 sets for rows, columns, and sub-boxes).
✨ Pro Tips for Coding Interviews
- Ask clarifying questions about constraints and input format.
- Explain your approach using sets for efficient validation.
- Discuss edge cases such as empty boards or nearly full invalid boards.
- Keep your code clean and readable with descriptive variable names.
0 Comments