36. Valid Sudoku
Valid Sudoku (LeetCode 36) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Previously Seen / Existence Check subpattern.
How to solve Valid Sudoku
Valid Sudoku is a Hashing for Existence Checks and Seen Tracking problem, part of Array/Matrix Manipulation Patterns. Use hash sets for O(1) existence checks. Track previously seen elements to detect duplicates, find pairs, and validate sequences.
Work towards the solution in these steps:
- A filled cell can conflict in exactly three places: its row, its column, and its 3x3 subgrid.
- Keep a record of which digits have already appeared in each row, column, and subgrid.
- The subgrid containing position `(row, column)` can be identified using integer division by 3.
The reference solution runs in O(1) time and O(1) space.
The Valid Sudoku solution and editorial walks through the full approach with reference code.
Problem statement
Implement isValidSudoku to determine whether the filled entries in board follow the rules of a 9x9 Sudoku grid. Each cell contains either a digit from '1' through '9' or '.', where '.' means the cell is empty.
Every digit that appears in a row must be different from all other digits in that row. The same requirement applies independently to each column and to each 3x3 subgrid; a subgrid is one of the nine regions formed by three consecutive rows and three consecutive columns.
Return true if no filled digit violates any row, column, or subgrid rule. Return false as soon as any duplicate is found. Empty cells are ignored, and the board does not need to be complete or solvable.
Example 1:
Input: board = [["4",".",".",".","7",".",".",".","."],[".",".","8",".",".",".",".","1","."],[".",".",".","2",".",".","6",".","."],[".","5",".",".",".","9",".",".","."],[".",".","1",".","3",".",".",".","7"],[".",".",".","6",".",".","4",".","."],[".","2",".",".",".",".",".","8","."],[".",".","6",".",".",".","3",".","."],[".",".",".","9",".",".",".",".","5"]] Output: true Explanation: Every filled digit is unique within its row, column, and 3x3 subgrid.
Example 2:
Input: board = [["4",".",".",".",".",".",".",".","."],[".","4",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]] Output: false Explanation: The two '4' cells lie in the same upper-left 3x3 subgrid, so that subgrid contains a duplicate.
Example 3:
Input: board = [[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".",".","."]] Output: true Explanation: There are no filled cells, so no row, column, or subgrid can contain a duplicate.
Constraints:
- board contains exactly 9 rows. - Every row in board contains exactly 9 cells. - Each cell is a one-character string equal to '.' or one of '1' through '9'. - Only filled cells are checked; empty cells represented by '.' impose no restriction. - The input board may be incomplete and is not guaranteed to have a solution.
Topics and companies
Topics: Array, Matrix, Hash Table, Hash Function.
How to practise Valid Sudoku on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) time and O(1) space.
Open Valid Sudoku in the code editor, or read the Valid Sudoku editorial for a worked solution with its approach and complexity analysis.
Where Valid Sudoku sits in the DSA pattern sheet
- Array/Matrix Manipulation Patterns pattern guide
- Hashing - Previously Seen / Existence Check subpattern
- The full DSA patterns sheet
Problems related to Valid Sudoku
Other problems that use the same Hashing - Previously Seen / Existence Check and Array/Matrix Manipulation Patterns techniques:
- Contains Duplicate — Easy, same subpattern
- Design HashSet — Easy, same subpattern
- Longest Consecutive Sequence — Medium, same subpattern
- Add Binary — Easy, same pattern
- Add to Array-Form of Integer — Easy, same pattern
- Concatenation of Array — Easy, same pattern
Valid Sudoku FAQ
What is the LeetCode number of Valid Sudoku?
Valid Sudoku is LeetCode problem 36, rated Medium.
What pattern is used to solve Valid Sudoku?
Valid Sudoku is a Hashing for Existence Checks and Seen Tracking problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Previously Seen / Existence Check subpattern.
What is the time complexity of Valid Sudoku?
The reference solution runs in O(1) time and O(1) space.