titan2400

Valid Sudoku - LeetCode

Nov 1st, 2025
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | Source Code | 0 0
  1. // Valid Sudoku - https://leetcode.com/problems/valid-sudoku/
  2.  
  3.  
  4. class Solution {
  5.  
  6.     // Brute Force (Three Grid Passes)
  7.     // Time Complexity: O(n^2)
  8.     // Space Complexity: O(n)
  9.     // public boolean isValidSudoku(char[][] board) {
  10.     //     // Check if rows are valid
  11.     //     for(int row = 0; row < board.length; row++) {
  12.     //         Set<Character> set = new HashSet<>();
  13.     //         for(int col = 0; col < board[0].length; col++) {
  14.     //             if(board[row][col] != '.') {
  15.     //                 if(!set.contains(board[row][col])) {
  16.     //                     set.add(board[row][col]);
  17.     //                 }
  18.     //                 else {
  19.     //                     System.out.println("Invalid Row: " + row);
  20.     //                     return false;
  21.     //                 }
  22.                    
  23.                    
  24.     //             }
  25.     //         }
  26.     //     }
  27.  
  28.  
  29.     //     // Check if columns are valid
  30.     //     for(int col = 0; col < board[0].length; col++) {
  31.     //         Set<Character> set = new HashSet<>();
  32.     //         for(int row = 0; row < board.length; row++) {
  33.     //             if(board[row][col] != '.') {
  34.     //                 if(!set.contains(board[row][col])) {
  35.     //                     set.add(board[row][col]);
  36.     //                 }
  37.     //                 else {
  38.     //                     System.out.println("Invalid Column: " + row);
  39.  
  40.     //                     return false;
  41.     //                 }
  42.     //             }
  43.     //         }
  44.     //     }
  45.  
  46.     //     // Check if inner 3x3 grids are valid
  47.     //     for(int boxRow = 0; boxRow < 3; boxRow++) {
  48.     //         for(int boxCol = 0; boxCol < 3; boxCol++) {
  49.     //             // inner grid elements
  50.     //             int startRow = boxRow * 3;
  51.     //             int startCol = boxCol * 3;
  52.     //             Set<Character> set = new HashSet<>();
  53.     //             for(int i = 0; i < 3; i++) {
  54.     //                 for(int j = 0; j < 3; j++) {
  55.  
  56.     //                     int r = startRow + i;
  57.     //                     int c = startCol + j;
  58.     //                     if(board[r][c] != '.') {
  59.     //                         if(!set.contains(board[r][c])) {
  60.     //                             set.add(board[r][c]);
  61.     //                         }
  62.     //                         else {
  63.     //                             return false;
  64.     //                         }
  65.     //                     }
  66.     //                 }
  67.     //             }
  68.     //         }
  69.     //     }
  70.  
  71.     //     return true;
  72.     // }
  73.  
  74.     // HashSet (One Pass)
  75.     // Time Complexity: O(n^2)
  76.     // Space Complexity: O(n^2)
  77.     // public boolean isValidSudoku(char[][] board) {
  78.     //     Map<Integer, Set<Character>> rows = new HashMap<>();
  79.     //     Map<Integer, Set<Character>> cols = new HashMap<>();
  80.     //     Map<String, Set<Character>> innerGrids = new HashMap<>();
  81.  
  82.     //     for(int row = 0; row < board.length; row++) {
  83.     //         for(int col = 0; col < board[0].length; col++) {
  84.     //             if(board[row][col] == '.') {
  85.     //                 continue;
  86.     //             }
  87.  
  88.     //             char el = board[row][col];
  89.  
  90.     //             String innerGrid = (row/3) + "," + (col/3);
  91.  
  92.     //             rows.computeIfAbsent(row, k -> new HashSet<>());
  93.     //             cols.computeIfAbsent(col, k -> new HashSet<>());
  94.     //             innerGrids.computeIfAbsent(innerGrid, k -> new HashSet<>());
  95.  
  96.     //             if(rows.get(row).contains(el)
  97.     //                 || cols.get(col).contains(el)
  98.     //                 || innerGrids.get(innerGrid).contains(el)) {
  99.     //                     return false;
  100.     //             }
  101.  
  102.     //             rows.get(row).add(el);
  103.     //             cols.get(col).add(el);
  104.     //             innerGrids.get(innerGrid).add(el);
  105.  
  106.     //         }
  107.     //     }
  108.  
  109.     //     return true;
  110.     // }
  111.  
  112.  
  113.     // BitMask (One Pass)
  114.     // Time Complexity: O(n^2)
  115.     // Space Complexity: O(n)
  116.     public boolean isValidSudoku(char[][] board) {
  117.         int[] rowMasks = new int[9];
  118.         int[] colMasks = new int[9];
  119.         int[] squareMasks = new int[9];
  120.  
  121.         for(int r = 0; r < 9; r++) {
  122.             for(int c = 0; c < 9; c++) {
  123.                 if(board[r][c] == '.') {
  124.                     continue;
  125.                 }
  126.  
  127.                 // 1. Convert char digit to a 0-indexed integer for bit positions.
  128.                 // '1' -> 0 (0th bit)
  129.                 // '2' -> 1 (1st bit)
  130.                 // ...
  131.                 // '9' -> 8 (8th bit)
  132.                 int digit = board[r][c] - '1';
  133.  
  134.                 // We use the left-shift operator (<<) to create an integer
  135.                 // with only the 'digit'-th bit turned on.
  136.                 // Example: If digit is '3', digitIndex is 2.
  137.                 // 1 << 2  -->  binary ...00000100  
  138.                 int bitToSet = 1 << digit;
  139.  
  140.                 int boxRow = (r/3);
  141.                 int boxCol = (c/3);
  142.                 int sqIndex = boxRow * 3 + boxCol;
  143.  
  144.                 // Check if bit has already been set
  145.                 // Has this digit been seen in this row before?
  146.                 // Has this digit been seen in this col before?
  147.                 // Has this digit been seen in this 3x3 square before?
  148.                 if((rowMasks[r] & bitToSet) > 0
  149.                     || (colMasks[c] & bitToSet) > 0
  150.                     || (squareMasks[sqIndex] & bitToSet) > 0) {
  151.                         return false;
  152.  
  153.                 }
  154.  
  155.                 rowMasks[r] |= bitToSet;
  156.                 colMasks[c] |= bitToSet;
  157.                 squareMasks[sqIndex] |= bitToSet;
  158.             }
  159.         }
  160.  
  161.         return true;
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment