File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Time Complexity: O(n^2)
2
+ // Space Complexity: O(n)
3
+
4
+ class Solution {
5
+ public:
6
+ bool isValidSudoku (vector<vector<char > > &board) {
7
+ bool used[9 ];
8
+
9
+ for (int i = 0 ; i < 9 ; ++i) {
10
+ fill (used, used + 9 , false );
11
+ for (int j = 0 ; j < 9 ; ++j) {
12
+ if (!check (board[i][j], used))
13
+ return false ;
14
+ }
15
+
16
+ fill (used, used + 9 , false );
17
+ for (int j = 0 ; j < 9 ; ++j) {
18
+ if (!check (board[j][i], used))
19
+ return false ;
20
+ }
21
+ }
22
+
23
+ for (int r = 0 ; r < 3 ; ++r) {
24
+ for (int c = 0 ; c < 3 ; ++c) {
25
+ fill (used, used + 9 , false );
26
+ for (int i = 3 * r; i < 3 * (r + 1 ); ++i) {
27
+ for (int j = 3 * c; j < 3 * (c + 1 ); ++j) {
28
+ if (!check (board[i][j], used))
29
+ return false ;
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ return true ;
36
+ }
37
+
38
+ private:
39
+ bool check (char c, bool used[9 ]) {
40
+ if (c != ' .' ) {
41
+ if (used[c - ' 1' ])
42
+ return false ;
43
+ used[c - ' 1' ] = true ;
44
+ }
45
+ return true ;
46
+ }
47
+ };
You can’t perform that action at this time.
0 commit comments