-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalid_sudoku.cpp
132 lines (104 loc) · 3.5 KB
/
valid_sudoku.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//more readable, less optimal
class Solution {
private:
bool isValidRow(vector<vector<char>>& board, int& row) {
set<int> digits; // SC: O(1)
for(int c = 0; c < 9; c++) {
if(digits.find(board[row][c]) != digits.end()) { // O(log(row_size))
return false;
}
if(board[row][c] != '.') {
digits.emplace(board[row][c]);
}
}
digits.clear();
return true;
}
bool isValidColumn(vector<vector<char>>& board, int& col) {
set<int> digits; // SC: O(1)
for(int r = 0; r < 9; r++) {
if(digits.find(board[r][col]) != digits.end()) { // O(log(row_size))
return false;
}
if(board[r][col] != '.') {
digits.emplace(board[r][col]);
}
}
digits.clear();
return true;
}
public:
bool isValidSudoku(vector<vector<char>>& board) {
//validate all rows
for(int r = 0; r < 9; r++) {
if(!isValidRow(board, r)) { //O(r)
return false;
}
}
//validate all cols
for(int c = 0; c < 9; c++) {
if(!isValidColumn(board, c)) { //O(r)
return false;
}
}
//valid 3x3 blocks
set<int> digits;
for(int blockRow = 0; blockRow < 3; blockRow++) {
for(int blockCol = 0; blockCol < 3; blockCol++) {
for(int r = 3 * blockRow; r < 3 * blockRow + 3; r++) {
for(int c = 3 * blockCol; c < 3 * blockCol + 3; c++) {
if(digits.find(board[r][c]) != digits.end()) {
return false;
}
if(board[r][c] != '.') {
digits.emplace(board[r][c]);
}
}
}
digits.clear();
}
}
return true;
}
};
//less readable, more optimal
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<bool> used(9, false);
for (int i = 0; i < 9; ++i) {
fill(used.begin(), used.end(), false);
// check row
for (int r = 0; r < 9; ++r) {
if (!check(board[r][i], used))
return false;
}
fill(used.begin(), used.end(), false);
// check col
for (int c = 0; c < 9; ++c) {
if (!check(board[i][c], used))
return false;
}
}
//for board
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
//for each square of 9*9 set the vector to false
fill(used.begin(), used.end(), false);
for(int r=3*i; r<3*i+3; r++) {
for(int c=3*j; c<3*j+3; c++) {
if(!check(board[r][c], used)) return false;
}
}
}
}
//when all cases evaluate to true...
return true;
}
bool check(char ch, vector<bool>& used) {
if (ch == '.') return true;
if (used[ch - '1']) return false;
used[ch - '1'] = true;
return true;
}
};