-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid Sudoku.java
115 lines (82 loc) · 3.43 KB
/
Valid Sudoku.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// https://leetcode.com/problems/valid-sudoku/
/*
Time Complexity: O(n^2) where n = number of elements inside board. We're checking all the elements in each row, column, and box.
Space Complexity: O(n) where n = number of elements inside set per row, column, and box. In my opinion, it's O(n) instead of
O(n^2) because each set is cleared per loop (not just nested loops).
*/
class Solution {
private boolean areColumnsValid(char[][] board) {
for (int i = 0; i < board.length; i++) {
Set<Character> uniqueElems = new HashSet<>();
for (int j = 0; j < board[i].length; j++) {
Character curChar = Character.valueOf(board[j][i]);
if (curChar.charValue() == '.') {
continue;
}
if (uniqueElems.contains(curChar)) {
return false;
}
uniqueElems.add(curChar);
}
}
return true;
}
private boolean areRowsValid(char[][] board) {
for (int i = 0; i < board.length; i++) {
Set<Character> uniqueElems = new HashSet<>();
for (int j = 0; j < board[i].length; j++) {
Character curChar = Character.valueOf(board[i][j]);
if (curChar.charValue() == '.') {
continue;
}
if (uniqueElems.contains(curChar)) {
return false;
}
uniqueElems.add(curChar);
}
}
return true;
}
private boolean isBoxValid(char[][] board, int row, int col) {
// IMPORTANT: This set has to be declared outside, unlike the others, because it's checking the boxes.
// If it was placed inside before the inner for-loop, it would only keep track of the elements on one row
// at a time per box. (i.e., set would have been clear once loop reached the 2nd loop of the inside of the
// box.)
Set<Character> uniqueElems = new HashSet<>();
for (int curRow = row; curRow < row + 3; curRow++) {
for (int curCol = col; curCol < col + 3; curCol++) {
Character curCharacter = Character.valueOf(board[curRow][curCol]);
if (curCharacter.charValue() == '.') {
continue;
}
if (uniqueElems.contains(curCharacter)) {
return false;
}
uniqueElems.add(curCharacter);
}
}
return true;
}
private boolean areBoxesValid(char[][] board) {
// IMPORTANT: Added "+= 3" because iterator would have enter a new box every 3 rows and columns.
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
// IMPORTANT: We're starting at the top-left corner of each box for validation
if (!isBoxValid(board, i, j)) {
return false;
}
}
}
return true;
}
public boolean isValidSudoku(char[][] board) {
// IMPORTANT: If either validation fails, immediately stop validation and return false
if (areBoxesValid(board)) {
if (areColumnsValid(board)) {
return areRowsValid(board);
}
return false;
}
return false;
}
}