(char[][] board)
| 1 | class Solution { |
| 2 | |
| 3 | public boolean isValidSudoku(char[][] board) { |
| 4 | //neetcode solution, slightly modified |
| 5 | |
| 6 | //a set of the characters that we have already come across (excluding '.' which denotes an empty space) |
| 7 | Set<Character> rowSet = null; |
| 8 | Set<Character> colSet = null; |
| 9 | |
| 10 | |
| 11 | for (int i = 0; i < 9; i++) { |
| 12 | //reinitialize the sets so we don't carry over found characters from the previous run |
| 13 | rowSet = new HashSet<>(); |
| 14 | colSet = new HashSet<>(); |
| 15 | for (int j = 0; j < 9; j++) { |
| 16 | char r = board[i][j]; |
| 17 | char c = board[j][i]; |
| 18 | if (r != '.'){ |
| 19 | if (rowSet.contains(r)){ |
| 20 | return false; |
| 21 | } else { |
| 22 | rowSet.add(r); |
| 23 | } |
| 24 | } |
| 25 | if (c != '.'){ |
| 26 | if (colSet.contains(c)){ |
| 27 | return false; |
| 28 | } else { |
| 29 | colSet.add(c); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | //block |
| 36 | //loop controls advance by 3 each time to jump through the boxes |
| 37 | for (int i = 0; i < 9; i = i + 3) { |
| 38 | for (int j = 0; j < 9; j = j + 3) { |
| 39 | //checkBlock will return true if valid |
| 40 | if (!checkBlock(i, j, board)) { |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | //passed all tests, therefore valid board |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | public boolean checkBlock(int idxI, int idxJ, char[][] board) { |
| 50 | Set<Character> blockSet = new HashSet<>(); |
nothing calls this directly
no test coverage detected