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

šŸ“š Reference Solution

ā–¼
⌘K Search āŒ˜ā†© Run ⌘S Submit