forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid Sudoku.cpp
63 lines (53 loc) · 1.64 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
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int n = board.size();
for(int i=0;i<n;i++){
unordered_map<char,int>m;
for(int j=0;j<board[i].size();j++){
if(board[i][j]=='.'){
continue;
}
if(m.find(board[i][j])!=m.end()){
return false;
}
else{
m[board[i][j]]++;
}
}
}
for(int i=0;i<n;i++){
unordered_map<char,int>m;
for(int j=0;j<n;j++){
if(board[j][i]=='.'){
continue;
}
if(m.find(board[j][i])!=m.end()){
return false;
}
else{
m[board[j][i]]++;
}
}
}
for(int i=0;i<n;i+=3){
for(int j=0;j<n;j+=3){
unordered_map<char,int>m;
for(int k=i;k<=i+2;k++){
for(int p=j;p<=j+2;p++){
if(board[k][p]=='.'){
continue;
}
if(m.find(board[k][p])!=m.end()){
return false;
}
else{
m[board[k][p]]++;
}
}
}
}
}
return true;
}
};