Leetcode 36: Valid Sudoku

grid47
grid47
Exploring patterns and algorithms
Nov 3, 2024 6 min read

A soft grid of numbers and symbols, gently highlighting valid sequences.
Solution to LeetCode 36: Valid Sudoku Problem

Determine if a 9x9 Sudoku board is valid. A valid Sudoku board follows three rules: Each row, column, and 3x3 sub-box must contain the digits 1-9 without repetition. Only the filled cells need to be validated.
Problem
Approach
Steps
Complexity
Input: A 9x9 grid representing the Sudoku board. Cells contain digits '1'-'9' or '.' for empty cells.
Example: Input: board = [["5", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], ...]
Constraints:
• board.length == 9
• board[i].length == 9
• Each cell is a digit from '1' to '9' or '.'
Output: Return 'true' if the board is valid according to Sudoku rules, otherwise return 'false'.
Example: Output: true
Constraints:
• Return a boolean value indicating whether the board is valid or not.
Goal: To validate the Sudoku board based on the three conditions: unique digits in rows, columns, and sub-boxes.
Steps:
• Iterate through each cell of the board.
• For each non-empty cell, check if the value already appears in the same row, column, or 3x3 sub-box.
• Use a set or map to track previously encountered numbers in each row, column, and sub-box.
• Return 'false' if a repeated number is found, otherwise return 'true' after checking all cells.
Goal: The board is always a 9x9 grid, and each cell contains either a digit from '1' to '9' or a '.' for an empty cell.
Steps:
• 1 <= board.length, board[i].length <= 9
• The input board contains only '1'-'9' or '.'
Assumptions:
• The board is a valid 9x9 grid.
• Only the filled cells are validated.
Input: Input: board = [["5", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], ...]
Explanation: The board is valid as no rows, columns, or sub-boxes have repeating digits. All filled cells follow Sudoku rules.

Input: Input: board = [["8", "3", ".", ".", "7", ".", ".", ".", "."], ["6", ".", ".", "1", "9", "5", ".", ".", "."], ...]
Explanation: The board is invalid because there are two '8's in the top-left 3x3 sub-box.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus