Valid Sudoku
To determine if a given 9 x 9 Sudoku board is valid, you must ensure that the numbers 19 do not repeat in any row, any column, or any of the nine 3 x 3 subgrids. Here’s a Python method to check the validity of the Sudoku board:


Explanation:
Rows: Iterate through each row and collect all the numbers, ignoring the ‘.’ character. If the set of numbers has a different length than the original list, it means there are duplicates, and the Sudoku is not valid.
Columns: The same logic as for rows but applied to columns.
3x3 Subgrids: Check all nine 3 x 3 subgrids by iterating through them with a step of 3. Collect all numbers in each subgrid and apply the same logic as for rows and columns.
The function returns True
if the Sudoku board is valid according to the given rules and False
otherwise.
Identifying Problem Isomorphism
“Determine if a 9x9 Sudoku Board is Valid” is an approximate isomorphism for “Valid Sudoku”.
In both problems, we are given a 9x9 Sudoku board, and the goal is to verify whether the given board is a valid Sudoku.
In “Valid Sudoku”, a Sudoku board is considered valid if all rows, columns, and 3x3 subboxes contain unique digits, and nonempty cells contain digits from 1 to 9.
Similarly, in “Determine if a 9x9 Sudoku Board is Valid”, the board is considered valid under the same conditions. However, this problem additionally specifies that empty cells are filled with the ‘.’ character.
The commonality is in the goal of verifying the validity of a Sudoku board using similar criteria. However, the slight variation in the representation of empty cells makes the mapping between the problems an approximate one.
They have similar complexities as they both require checking each row, column, and 3x3 subbox of the Sudoku board. Therefore, neither problem is inherently simpler or more complex than the other.
“Valid Sudoku” involves understanding of the rules of Sudoku and how to apply them to check a given 9x9 grid. Here are 10 problems to prepare for this problem:
Two Sum (LeetCode 1): This problem involves hashing and can help you to learn how to keep track of seen numbers, a skill useful in “Valid Sudoku”.
Contains Duplicate (LeetCode 217): This problem is about finding if an array contains any duplicates which introduces the idea of keeping track of seen elements.
Single Number (LeetCode 136): This problem can help you learn how to deal with numbers in an array where each element appears twice except for one that appears once.
Find All Numbers Disappeared in an Array (LeetCode 448): This problem requires understanding of how to work with arrays and find missing numbers, which is useful in the context of Sudoku.
First Unique Character in a String (LeetCode 387): This problem asks for finding the first nonrepeating character in a string and can be solved with a hash map, similarly to “Valid Sudoku”.
Intersection of Two Arrays (LeetCode 349): This problem introduces you to the concept of finding common elements between two arrays, which may aid in understanding how to check rows, columns, and boxes in Sudoku.
Find the Difference (LeetCode 389): This problem can help you practice dealing with characters and their frequencies, a similar concept to checking numbers in Sudoku.
Missing Number (LeetCode 268): This problem involves finding the missing number in an array, which might be useful in the context of a Sudoku problem.
Rotate Array (LeetCode 189): This problem could be helpful for understanding how to manipulate arrays, a necessary skill for solving Sudoku.
Isomorphic Strings (LeetCode 205): This problem involves finding a way to map one string to another, which can help you understand how to map Sudoku row and column indices to box numbers.


Problem Classification
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 19 without repetition. Each column must contain the digits 19 without repetition. Each of the nine 3 x 3 subboxes of the grid must contain the digits 19 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.
Example 1:
Input: board = [[“5”,“3”,".",".",“7”,".",".",".","."] ,[“6”,".",".",“1”,“9”,“5”,".",".","."] ,[".",“9”,“8”,".",".",".",".",“6”,"."] ,[“8”,".",".",".",“6”,".",".",".",“3”] ,[“4”,".",".",“8”,".",“3”,".",".",“1”] ,[“7”,".",".",".",“2”,".",".",".",“6”] ,[".",“6”,".",".",".",".",“2”,“8”,"."] ,[".",".",".",“4”,“1”,“9”,".",".",“5”] ,[".",".",".",".",“8”,".",".",“7”,“9”]] Output: true Example 2:
Input: board = [[“8”,“3”,".",".",“7”,".",".",".","."] ,[“6”,".",".",“1”,“9”,“5”,".",".","."] ,[".",“9”,“8”,".",".",".",".",“6”,"."] ,[“8”,".",".",".",“6”,".",".",".",“3”] ,[“4”,".",".",“8”,".",“3”,".",".",“1”] ,[“7”,".",".",".",“2”,".",".",".",“6”] ,[".",“6”,".",".",".",".",“2”,“8”,"."] ,[".",".",".",“4”,“1”,“9”,".",".",“5”] ,[".",".",".",".",“8”,".",".",“7”,“9”]] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8’s in the top left 3x3 subbox, it is invalid.
Constraints:
board.length == 9 board[i].length == 9 board[i][j] is a digit 19 or ‘.’.
Language Agnostic Coding Drills
The code is a solution to the problem of validating a Sudoku board, using the rules of Sudoku. Below are the key elements in this solution that one needs to understand, broken down into the smallest units of learning. They are listed in order of increasing difficulty.
Basic Syntax and Structure: Understand how to define a class and methods, and how to use loops and conditional statements.
Lists and List Manipulation: Learn how to create, add elements to, and manipulate lists.
Basic Data Types: Understand the string and integer data types, and how to check for equality and inequality.
Working with 2D Arrays: Learn how to traverse and access elements of a 2D array (like the Sudoku board in this case).
Tuples: Understand how to create and use tuples. In this case, tuples are used to represent the row, column, and value of each cell in the Sudoku board.
List Comprehension: Understand how to use list comprehension to add elements to a list in a single line of code.
Division Operator: Understand the difference between normal division (
/
) and floor division (//
), as well as when and why to use each.Sets and Set Operations: Learn how to create sets, and understand the concept of uniqueness in sets. In this case, a set is used to eliminate duplicate elements.
Boolean Operations: Understand how to use boolean operators (
==
) to compare values and return a boolean result.ProblemSolving: Understand the rules of Sudoku and how to check for a valid Sudoku solution. Learn to conceptualize the problem and develop an algorithmic solution.
Once the above concepts are understood and can be coded separately, they can be combined to form the final solution to the Sudoku validation problem.
Targeted Drills in Python
 Basic Syntax and Structure: Create a class with a method that prints “Hello, World!”.


 Lists and List Manipulation: Create a list, add elements to it, and print it.


 Basic Data Types: Check if two strings are equal and print the result.


 Working with 2D Arrays: Create a 2D array and print the element at row 2, column 3.


 Tuples: Create a tuple, add elements to it, and print it.


 List Comprehension: Create a list of squares of numbers from 0 to 9.


 Division Operator: Divide 7 by 3 using both normal division and floor division, and print the results.


 Sets and Set Operations: Create a set, add elements to it, and print it.


 Boolean Operations: Compare whether 5 is greater than 3 and print the result.


 ProblemSolving: Create a drill to understand the rules of Sudoku. Start with a 3x3 Sudoku grid and write a program that checks if it is valid.


This solution only checks for uniqueness in each row and column but doesn’t check the subgrids. For the original problem, you’d need to add a check for each 3x3 subgrid as well. This should be implemented once the learner is comfortable with the concepts explained above.