Skip to content

visual walkthrough

Valid Sudoku

MediumHash Set per GroupReported at: AmazonMicrosoftMeta+3

Encoding row, column and box membership in sets; clean hashing of 2D data.

Solve on LeetCode

The idea

A board is valid when no row, no column and no 3×3 box contains the same digit twice. You don't need to solve it, only to look for repeats.

Giving every row, column and box its own set lets one pass over the grid catch a repeat the moment it appears.

Complexity

approachtimespace
Check each groupO(81 × 3)O(9)
One pass, three setsO(81)O(81)

More walkthroughs