|
| 1 | +package in.knowledgegate.dsa.array; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +/** |
| 7 | + * Determine if a 9 x 9 Sudoku board is valid. Only |
| 8 | + * the filled cells need to be validated according |
| 9 | + * to the following rules: |
| 10 | + * |
| 11 | + * Each row must contain the digits 1-9 without repetition. |
| 12 | + * Each column must contain the digits 1-9 without |
| 13 | + * repetition. |
| 14 | + * Each of the nine 3 x 3 sub-boxes of the grid must |
| 15 | + * contain the digits 1-9 without repetition. |
| 16 | + * Note: |
| 17 | + * |
| 18 | + * A Sudoku board (partially filled) could be valid |
| 19 | + * but is not necessarily solvable. |
| 20 | + * Only the filled cells need to be validated |
| 21 | + * according to the mentioned rules. |
| 22 | + * |
| 23 | + * |
| 24 | + * Example 1: |
| 25 | + * Input: board = |
| 26 | + * { {'5','3','.','.','7','.','.','.','.'} |
| 27 | + * , {'6','.','.','1','9','5','.','.','.'} |
| 28 | + * , {'.','9','8','.','.','.','.','6','.'} |
| 29 | + * , {'8','.','.','.','6','.','.','.','3'} |
| 30 | + * , {'4','.','.','8','.','3','.','.','1'} |
| 31 | + * , {'7','.','.','.','2','.','.','.','6'} |
| 32 | + * , {'.','6','.','.','.','.','2','8','.'} |
| 33 | + * , {'.','.','.','4','1','9','.','.','5'} |
| 34 | + * , {'.','.','.','.','8','.','.','7','9'}} |
| 35 | + * Output: true |
| 36 | + * |
| 37 | + * Example 2: |
| 38 | + * Input: board = |
| 39 | + * [["8","3",".",".","7",".",".",".","."] |
| 40 | + * ,["6",".",".","1","9","5",".",".","."] |
| 41 | + * ,[".","9","8",".",".",".",".","6","."] |
| 42 | + * ,["8",".",".",".","6",".",".",".","3"] |
| 43 | + * ,["4",".",".","8",".","3",".",".","1"] |
| 44 | + * ,["7",".",".",".","2",".",".",".","6"] |
| 45 | + * ,[".","6",".",".",".",".","2","8","."] |
| 46 | + * ,[".",".",".","4","1","9",".",".","5"] |
| 47 | + * ,[".",".",".",".","8",".",".","7","9"]] |
| 48 | + * Output: false |
| 49 | + * |
| 50 | + * Explanation: Same as Example 1, except with the |
| 51 | + * 5 in the top left corner being modified to 8. |
| 52 | + * Since there are two 8's in the top left 3x3 |
| 53 | + * sub-box, it is invalid. |
| 54 | + * |
| 55 | + * |
| 56 | + * Constraints: |
| 57 | + * board.length == 9 |
| 58 | + * board[i].length == 9 |
| 59 | + * board[i][j] is a digit 1-9 or '.'. |
| 60 | + */ |
| 61 | +public class ValidSudoku { |
| 62 | + public static void main(String[] args) { |
| 63 | + ValidSudoku checker = new ValidSudoku(); |
| 64 | + char[][] puzzle = new char[][] |
| 65 | + {{'5','3','.','.','7','.','.','.','.'} |
| 66 | + ,{'6','.','.','1','9','5','.','.','.'} |
| 67 | + ,{'.','9','8','.','.','.','.','6','.'} |
| 68 | + ,{'8','.','.','.','6','.','.','.','3'} |
| 69 | + ,{'4','.','.','8','.','3','.','.','1'} |
| 70 | + ,{'7','.','.','.','2','.','.','.', '6'} |
| 71 | + ,{'.','6','.','.','.','.','2','8','.'} |
| 72 | + ,{'.','.','.','4','1','9','.','.','5'} |
| 73 | + ,{'.','.','.','.','8','.','.', '7','9'}}; |
| 74 | + System.out.println("IsValid:" + checker.isValidSudoku(puzzle)); |
| 75 | + } |
| 76 | + |
| 77 | + public boolean isValidSudoku(char[][] board) { |
| 78 | + Set<String> visited = new HashSet<>(); |
| 79 | + for (int i = 0; i < 9; i++) { |
| 80 | + for (int j = 0; j < 9; j++) { |
| 81 | + char c = board[i][j]; |
| 82 | + if (c == '.') continue; |
| 83 | + if (!visited.add(c +"row" + i) || |
| 84 | + !visited.add(c + "col" + j) || |
| 85 | + !visited.add(c + "box" + i/3 + "-" + j/3)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return true; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments