forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path36. Valid Sudoku.cpp
47 lines (47 loc) · 1.43 KB
/
36. 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
class Solution
{
public:
bool checkSq(vector<vector<char>> &board, int r, int c)
{
unordered_set<int> s1;
int count(0);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[r + i][c + j] == '.')
continue;
count++;
s1.insert(board[r + i][c + j]);
}
}
return count == s1.size();
}
bool isValidSudoku(vector<vector<char>> &board)
{
unordered_map<char, int> rows[9], cols[9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (board[i][j] == '.')
continue;
rows[i][board[i][j]]++;
if (rows[i][board[i][j]] == 2)
{
cout << i << " " << j << endl;
return false;
}
cols[j][board[i][j]]++;
if (cols[j][board[i][j]] == 2)
{
cout << i << " " << j << endl;
return false;
}
}
}
if (!checkSq(board, 0, 0) || !checkSq(board, 0, 3) || !checkSq(board, 0, 6) || !checkSq(board, 3, 0) || !checkSq(board, 3, 3) || !checkSq(board, 3, 6) || !checkSq(board, 6, 0) || !checkSq(board, 6, 3) || !checkSq(board, 6, 6))
return false;
return true;
}
};