Valid Sudoku - Complete Solution Guide
Valid Sudoku is LeetCode problem 36, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules : Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. 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 according to the mentioned rules. Exa
Detailed Explanation
The problem 'Valid Sudoku' asks us to determine whether a given 9x9 Sudoku board is valid. A Sudoku board is valid if the following three conditions are met: 1. Each row contains the digits 1-9 without any repetition. 2. Each column contains the digits 1-9 without any repetition. 3. Each of the nine 3x3 sub-boxes (also called blocks or regions) of the grid contains the digits 1-9 without any repetition. It's important to note that the input Sudoku board might be partially filled, meaning it can contain empty cells represented by the character '.'. We only need to check if the filled cells violate the Sudoku rules. The board is not necessarily solvable, just needs to be a valid configuration so far.
Solution Approach
The provided solution uses a single set (`seen`) to keep track of the numbers encountered in each row, column, and 3x3 sub-box. For each cell in the board, it checks if the cell contains a digit (not a '.'). If it does, it generates three unique keys: one for the row, one for the column, and one for the 3x3 sub-box the cell belongs to. These keys include the row index, column index, and sub-box indices, and the digit in the cell. Before adding these keys to the `seen` set, the solution checks if any of them are already present. If any key is already in the set, it means that the same digit has been encountered before in the same row, column, or sub-box, violating the Sudoku rules. If all keys are new, they are added to the set, and the algorithm continues. If no violations are found after iterating through all the cells, the board is valid.
Step-by-Step Algorithm
- Step 1: Initialize an empty set called `seen` to store the keys.
- Step 2: Iterate through each cell of the 9x9 Sudoku board using nested loops.
- Step 3: For each cell, check if it contains a digit (i.e., it's not a '.').
- Step 4: If the cell contains a digit, generate three unique keys:
- a. `row_key`: Represents the row constraint (e.g., 'row05' indicates the digit '5' in row 0).
- b. `col_key`: Represents the column constraint (e.g., 'col15' indicates the digit '5' in column 1).
- c. `box_key`: Represents the 3x3 sub-box constraint (e.g., 'box005' indicates the digit '5' in the top-left sub-box).
- Step 5: Check if any of the generated keys are already present in the `seen` set. If any key is found, it means there's a duplicate digit in that row, column, or sub-box, and the Sudoku board is invalid. Return `false`.
- Step 6: If all three keys are new (not found in the `seen` set), add them to the `seen` set.
- Step 7: After iterating through all the cells, if no duplicates were found, the Sudoku board is valid. Return `true`.
Key Insights
- Insight 1: We need to efficiently check for the presence of a digit within a row, column, and 3x3 sub-box.
- Insight 2: Using a set (or hash table) is an efficient way to track which numbers have already been seen in a row, column, or sub-box. Its fast O(1) lookup is crucial.
- Insight 3: Representing the row, column, and sub-box constraints as unique strings (or tuples) allows a single set to manage validation across all three rules simultaneously.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)
Topics
This problem involves: Array, Hash Table, Matrix.
Companies
Asked at: Adobe, Amazon, Apple, Attentive, Autodesk, Bloomberg, Confluent, DoorDash, Geico, Goldman Sachs, Instacart, Karat, Media.net, Meta, Microsoft, MongoDB, Nvidia, Oracle, PayPal, Riot Games, Samsara, Snap, TikTok, Uber, Veeva Systems, Visa, Walmart Labs, Waymo, Yahoo, Zoho.