MEDIUM NC#7 Arrays & Hashing
36. Valid Sudoku
š Problem
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: 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 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition. Note: A Sudoku board (partially filled) could be valid but is not necessarily solvable. Only the filled cells need to be validated.
š§ Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- ā¢Array methods (`push`, `pop`, `shift`, `slice`)
- ā¢Object/Map/Set usage patterns
- ā¢Function parameter and return typing
Logical Thinking Concepts
- ā¢Define invariants before coding
- ā¢Check edge cases first (`[]`, single element, duplicates)
- ā¢Estimate time/space before implementation
š” Approach
- ā Use sets for rows, columns, and 3x3 boxes
- ā Box index = (row/3) * 3 + (col/3)
- ā Check for duplicates as we iterate through board
- ā Time: O(n²), Space: O(n²) where n = 9
š§ Prerequisites
š ļø Hints & Pitfalls
Hints
- ā¢Use sets for rows, columns, and 3x3 boxes
- ā¢Box index = (row/3) * 3 + (col/3)
- ā¢Check for duplicates as we iterate through board
Common Pitfalls
- ā¢Time: O(n²), Space: O(n²) where n = 9
š§Ŗ Test Cases
Test Case 1
Not run Input:
isValidSudoku(board1); Expected:
true Test Case 2
Not run Input:
isValidSudoku(board2); Expected:
false Test Case 3
Not run Input:
isValidSudoku(board: string[][]); Expected:
Computed from hidden reference š Code Editor
š¤ Output