Leetcode 36: Valid Sudoku
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.
Approach: Use a set or map to track the digits that have appeared in each row, column, and 3x3 sub-box to ensure there are no duplicates.
Observations:
• We can use sets or maps to efficiently track the digits we encounter in rows, columns, and sub-boxes.
• Using a single iteration through the board and checking each non-empty cell will ensure an efficient solution.
Steps:
• For each non-empty cell, check if its value already exists in the corresponding row, column, or sub-box using sets.
• If a duplicate is found, return 'false'.
• If no duplicates are found after checking all cells, return 'true'.
Empty Inputs:
• The board may contain empty cells represented by '.' which should be ignored during validation.
Large Inputs:
• N/A, as the board size is fixed at 9x9.
Special Values:
• Consider cases where all rows, columns, and sub-boxes have exactly one number or are filled with duplicates.
Constraints:
• Ensure the board is always 9x9 as per the constraints.
bool isValidSudoku(vector<vector<char>>& board) {
map<string, bool> ma;
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++) {
if(board[i][j] != '.') {
string key = "(";
key+=board[i][j];
key+=")";
if(ma.count(to_string(i)+key)) return false;
else ma[to_string(i)+key] = true;
// cout << to_string(i)+key;
if(ma.count(key+to_string(j))) return false;
else ma[key+to_string(j)] = true;
// cout << key+to_string(j);
int x = i / 3, y = j / 3;
if(ma.count(to_string(x)+ key+ to_string(y))) return false;
else ma[to_string(x)+ key+ to_string(y)] = true;
// cout<< to_string(i)+ key+ to_string(j);
}
}
return true;
}
1 : Function Declaration
bool isValidSudoku(vector<vector<char>>& board) {
This line declares a function named 'isValidSudoku' that takes a 2D vector 'board' representing the Sudoku board as input and returns a boolean indicating whether the board is valid.
2 : Map Initialization
map<string, bool> ma;
This line initializes an empty map 'ma' to store information about the numbers seen in each row, column, and 3x3 sub-grid.
3 : Nested Loops
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++) {
This nested loop iterates over each cell of the Sudoku board.
4 : Conditional Check
if(board[i][j] != '.') {
This line checks if the current cell is not empty.
5 : String Manipulation
string key = "(";
key+=board[i][j];
key+=")";
This block creates a unique key for the current number, row, or column by concatenating the number with the row or column index enclosed in parentheses.
6 : Map Operations
if(ma.count(to_string(i)+key)) return false;
This line checks if the current number has already been seen in the current row. If so, it means the Sudoku is invalid, and the function returns false.
7 : Map Operations
else ma[to_string(i)+key] = true;
If the number hasn't been seen in the current row, it's added to the map with a key representing the row and number.
8 : Map Operations
if(ma.count(key+to_string(j))) return false;
This line checks if the current number has already been seen in the current column. If so, it means the Sudoku is invalid, and the function returns false.
9 : Map Operations
else ma[key+to_string(j)] = true;
If the number hasn't been seen in the current column, it's added to the map with a key representing the column and number.
10 : Calculation
int x = i / 3, y = j / 3;
This line calculates the indices of the 3x3 sub-grid to which the current cell belongs.
11 : Map Operations
if(ma.count(to_string(x)+ key+ to_string(y))) return false;
This line checks if the current number has already been seen in the current 3x3 sub-grid. If so, it means the Sudoku is invalid, and the function returns false.
12 : Map Operations
else ma[to_string(x)+ key+ to_string(y)] = true;
If the number hasn't been seen in the current 3x3 sub-grid, it's added to the map with a key representing the sub-grid indices and number.
13 : Return
return true;
If the loop completes without finding any violations, the function returns true, indicating that the Sudoku board is valid.
Best Case: O(1)
Average Case: O(n^2)
Worst Case: O(n^2)
Description: Time complexity is O(n^2) because we are iterating through a 9x9 grid.
Best Case: O(1)
Worst Case: O(n)
Description: The space complexity is O(n) due to the use of sets to track digits for rows, columns, and sub-boxes.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus