|
| 1 | +--- |
| 2 | +id: sudoku-solver |
| 3 | +title: Sudoku Solver |
| 4 | +difficulty: Hard |
| 5 | +sidebar_label: 0037-ValidSudoku |
| 6 | +tags: |
| 7 | + - Array |
| 8 | + - Hash Table |
| 9 | + - Matrix |
| 10 | + - Backtracking |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 16 | +| :---------------- | :------------ | :--------------- | |
| 17 | +| [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/description/) | [Sudoku Solver Solution on LeetCode](https://leetcode.com/problems/sudoku-solver/solutions/) | [Debangi Ghosh](https://leetcode.com/u/debangi_29/) | |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +Write a program to solve a Sudoku puzzle by filling the empty cells. |
| 22 | + |
| 23 | +A sudoku solution must satisfy all of the following rules: |
| 24 | + |
| 25 | +1. Each of the digits 1-9 must occur exactly once in each row. |
| 26 | +2. Each of the digits 1-9 must occur exactly once in each column. |
| 27 | +3. Each of the digits 1-9 must occur exactly once in each of the 9 $3 \times 3$ sub-boxes of the grid. |
| 28 | + |
| 29 | +The '.' character indicates empty cells. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +### Examples |
| 34 | + |
| 35 | +#### Example 1: |
| 36 | + |
| 37 | +**Input**: board = |
| 38 | +``` |
| 39 | +[["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"]] |
| 40 | +``` |
| 41 | + |
| 42 | +**Output**: |
| 43 | +``` |
| 44 | +[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]] |
| 45 | +``` |
| 46 | + |
| 47 | +**Explanation**: The input board is shown above and the only valid solution is shown below: |
| 48 | + |
| 49 | + |
| 50 | +### Constraints |
| 51 | + |
| 52 | +- board.length == 9 |
| 53 | +- board[i].length == 9 |
| 54 | +- board[i][j] is a digit 1-9 or '.'. |
| 55 | +- It is guaranteed that the input board has only one solution. |
| 56 | + |
| 57 | +### Approach |
| 58 | + |
| 59 | +👉 Iterate Through Each Cell: The algorithm iterates through each cell of the Sudoku board |
| 60 | + |
| 61 | +👉 Empty Cell Check: If the current cell is empty (contains ‘.’), the algorithm proceeds to try different numbers (from ‘1’ to ‘9’) in that cell |
| 62 | + |
| 63 | +👉 Number Placement: For each number, the algorithm checks if placing that number in the current cell is valid according to the Sudoku rules (no repetition in the same row, column, or $3 \times 3$ sub-box). |
| 64 | + |
| 65 | +👉 Recursive Exploration: If placing a number is valid, the algorithm sets that number in the cell and recursively explores the next empty cell. |
| 66 | + |
| 67 | +👉 Backtracking: If the recursive exploration leads to an invalid solution, the algorithm backtracks by undoing the choice (resetting the cell to ‘.’) and trying the next number. |
| 68 | + |
| 69 | +👉 Solution Check: The algorithm continues exploring possibilities until a valid solution is found, or it exhaustively searches all possibilities. |
| 70 | + |
| 71 | +### Solution Code |
| 72 | + |
| 73 | +#### Python |
| 74 | + |
| 75 | +``` |
| 76 | +class Solution: |
| 77 | + def solveSudoku(self, board: List[List[str]]) -> None: |
| 78 | + def isValid(row: int, col: int, c: str) -> bool: |
| 79 | + for i in range(9): |
| 80 | + if board[i][col] == c or \ |
| 81 | + board[row][i] == c or \ |
| 82 | + board[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == c: |
| 83 | + return False |
| 84 | + return True |
| 85 | +
|
| 86 | + def solve(s: int) -> bool: |
| 87 | + if s == 81: |
| 88 | + return True |
| 89 | +
|
| 90 | + i = s // 9 |
| 91 | + j = s % 9 |
| 92 | +
|
| 93 | + if board[i][j] != '.': |
| 94 | + return solve(s + 1) |
| 95 | +
|
| 96 | + for c in string.digits[1:]: |
| 97 | + if isValid(i, j, c): |
| 98 | + board[i][j] = c |
| 99 | + if solve(s + 1): |
| 100 | + return True |
| 101 | + board[i][j] = '.' |
| 102 | +
|
| 103 | + return False |
| 104 | +
|
| 105 | + solve(0) |
| 106 | +
|
| 107 | +``` |
| 108 | + |
| 109 | +#### Java |
| 110 | + |
| 111 | +``` |
| 112 | +class Solution { |
| 113 | + public void solveSudoku(char[][] board) { |
| 114 | + dfs(board, 0); |
| 115 | + } |
| 116 | +
|
| 117 | + private boolean dfs(char[][] board, int s) { |
| 118 | + if (s == 81) |
| 119 | + return true; |
| 120 | +
|
| 121 | + final int i = s / 9; |
| 122 | + final int j = s % 9; |
| 123 | +
|
| 124 | + if (board[i][j] != '.') |
| 125 | + return dfs(board, s + 1); |
| 126 | +
|
| 127 | + for (char c = '1'; c <= '9'; ++c) |
| 128 | + if (isValid(board, i, j, c)) { |
| 129 | + board[i][j] = c; |
| 130 | + if (dfs(board, s + 1)) |
| 131 | + return true; |
| 132 | + board[i][j] = '.'; |
| 133 | + } |
| 134 | +
|
| 135 | + return false; |
| 136 | + } |
| 137 | +
|
| 138 | + private boolean isValid(char[][] board, int row, int col, char c) { |
| 139 | + for (int i = 0; i < 9; ++i) |
| 140 | + if (board[i][col] == c || board[row][i] == c || |
| 141 | + board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) |
| 142 | + return false; |
| 143 | + return true; |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +#### C++ |
| 149 | + |
| 150 | +``` |
| 151 | +class Solution { |
| 152 | + public: |
| 153 | + void solveSudoku(vector<vector<char>>& board) { |
| 154 | + solve(board, 0); |
| 155 | + } |
| 156 | +
|
| 157 | + private: |
| 158 | + bool solve(vector<vector<char>>& board, int s) { |
| 159 | + if (s == 81) |
| 160 | + return true; |
| 161 | +
|
| 162 | + const int i = s / 9; |
| 163 | + const int j = s % 9; |
| 164 | +
|
| 165 | + if (board[i][j] != '.') |
| 166 | + return solve(board, s + 1); |
| 167 | +
|
| 168 | + for (char c = '1'; c <= '9'; ++c) |
| 169 | + if (isValid(board, i, j, c)) { |
| 170 | + board[i][j] = c; |
| 171 | + if (solve(board, s + 1)) |
| 172 | + return true; |
| 173 | + board[i][j] = '.'; |
| 174 | + } |
| 175 | +
|
| 176 | + return false; |
| 177 | + } |
| 178 | +
|
| 179 | + bool isValid(vector<vector<char>>& board, int row, int col, char c) { |
| 180 | + for (int i = 0; i < 9; ++i) |
| 181 | + if (board[i][col] == c || board[row][i] == c || |
| 182 | + board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) |
| 183 | + return false; |
| 184 | + return true; |
| 185 | + } |
| 186 | +}; |
| 187 | +
|
| 188 | +``` |
| 189 | + |
| 190 | +### Conclusion |
| 191 | + |
| 192 | +✅ Time Complexity: |
| 193 | + |
| 194 | +✔ The solve method uses a backtracking approach to explore the solution space. |
| 195 | + |
| 196 | +✔ In the worst case, the algorithm tries out all possibilities for each empty cell until a valid solution is found or all possibilities are exhausted. |
| 197 | + |
| 198 | +✔ The time complexity of the backtracking algorithm is typically exponential, but in practice, it tends to be much less than the worst case. |
| 199 | + |
| 200 | +✔ Therefore, we often express the time complexity of backtracking algorithms using Big O notation as $O(b^d)$, where b is the branching factor (average number of choices at each decision point) and d is the depth of the recursion tree. |
| 201 | + |
| 202 | +✅ Space Complexity: |
| 203 | + |
| 204 | +✔ The space complexity is determined by the recursive call stack during the backtracking process. |
| 205 | + |
| 206 | +✔ In the worst case, the depth of the recursion tree can be equal to the number of empty cells on the Sudoku board. |
| 207 | + |
| 208 | +✔ Therefore, the space complexity is $O(bd)$, where b is the branching factor and d is the depth of the recursion tree. |
0 commit comments