|
| 1 | +// LeetCode: https://leetcode.com/problems/valid-sudoku/ |
| 2 | +// --------------------------------------------------------- |
| 3 | +// Problem Statement: |
| 4 | +// Determine if a 9 x 9 Sudoku board is valid. |
| 5 | +// Only the filled cells need to be validated according to these rules: |
| 6 | +// 1. Each row must contain the digits 1-9 without repetition. |
| 7 | +// 2. Each column must contain the digits 1-9 without repetition. |
| 8 | +// 3. Each of the nine 3 x 3 sub-boxes must contain the digits 1-9 without repetition. |
| 9 | +// Note: A Sudoku board (partially filled) could be valid but is not necessarily solvable. |
| 10 | +// |
| 11 | +// --------------------------------------------------------- |
| 12 | +// Approach: |
| 13 | +// - Use a HashSet to track digits in each row, column, and 3x3 sub-box. |
| 14 | +// - For rows: Iterate row-wise and check duplicates. |
| 15 | +// - For columns: Iterate col-wise and check duplicates. |
| 16 | +// - For 3x3 sub-boxes: Divide board into 9 blocks using (blockRow, blockCol) |
| 17 | +// and iterate inside each 3x3 block. |
| 18 | +// - If at any point a duplicate is found, return false. |
| 19 | +// - If all checks pass, return true. |
| 20 | +// |
| 21 | +// --------------------------------------------------------- |
| 22 | +// Complexity Analysis: |
| 23 | +// Time Complexity: O(9^2) = O(81) ≈ O(1) |
| 24 | +// - We scan each cell at most 3 times (row, col, box). |
| 25 | +// - Constant-time operations in sets. |
| 26 | +// - Overall complexity is effectively constant since board size is fixed. |
| 27 | +// |
| 28 | +// Space Complexity: O(9) per row/col/box = O(1) |
| 29 | +// - Extra space comes from the HashSet used in each validation step. |
| 30 | +// - Board size is fixed, so extra memory usage is constant. |
| 31 | +// |
| 32 | +// --------------------------------------------------------- |
| 33 | +// Notes for Future: |
| 34 | +// - This solution validates *only* the current board state. It does not attempt to solve the Sudoku. |
| 35 | +// - If extending this to a Sudoku solver, backtracking would be required. |
| 36 | +// --------------------------------------------------------- |
| 37 | + |
| 38 | +import java.util.HashSet; |
| 39 | +import java.util.Set; |
| 40 | + |
| 41 | +class Solution { |
| 42 | + public boolean isValidSudoku(char[][] board) { |
| 43 | + |
| 44 | + int m = board.length; |
| 45 | + int n = board[0].length; |
| 46 | + |
| 47 | + // Check rows |
| 48 | + for (int i = 0; i < m; i++) { |
| 49 | + Set<Character> set = new HashSet<>(); |
| 50 | + for (int j = 0; j < n; j++) { |
| 51 | + char ch = board[i][j]; |
| 52 | + if (ch != '.') { |
| 53 | + if (set.contains(ch)) |
| 54 | + return false; |
| 55 | + else |
| 56 | + set.add(ch); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Check cols |
| 62 | + for (int i = 0; i < m; i++) { |
| 63 | + Set<Character> set = new HashSet<>(); |
| 64 | + for (int k = 0; k < m; k++) { |
| 65 | + char ch = board[k][i]; |
| 66 | + if (ch != '.') { |
| 67 | + if (set.contains(ch)) |
| 68 | + return false; |
| 69 | + else |
| 70 | + set.add(ch); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Check 3x3 sub-boxes |
| 76 | + for (int blockRow = 0; blockRow < 3; blockRow++) { |
| 77 | + for (int blockCol = 0; blockCol < 3; blockCol++) { |
| 78 | + Set<Character> set = new HashSet<>(); |
| 79 | + for (int i = 0; i < 3; i++) { |
| 80 | + for (int j = 0; j < 3; j++) { |
| 81 | + char ch = board[blockRow * 3 + i][blockCol * 3 + j]; |
| 82 | + if (ch != '.') { |
| 83 | + if (set.contains(ch)) |
| 84 | + return false; |
| 85 | + set.add(ch); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | +} |
0 commit comments