-
Notifications
You must be signed in to change notification settings - Fork 0
/
36. Valid Sudoku
66 lines (64 loc) · 2.11 KB
/
36. Valid Sudoku
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
//🎯DAY 17 PROBLEM 1
//The three checks are done using for loops, i.e row check, col check and square check .
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int count[11]={0};
for(int i=0;i<board.size();i++)
{
//after every coloumn, we set the count array 0, as for every coloumn we have to check for repetition of 1-9
memset(count,0,sizeof(count));
for(int j=0;j<board[i].size();j++)
{
char num=board[i][j];
if(num!='.')
{
//ascii code of 0 is 48 so to avoid out of bound condition i.e. to make every index under 10 , we subtract 48 from board[i][j]
count[board[i][j]-48]++;
//if frequency of any character is >1, then return false i.e. sudoku is not valid
if(count[board[i][j]-48]>1)
{
return false;
}
}
}
}
memset(count,0,sizeof(count));
for(int i=0;i<board.size();i++)
{
memset(count,0,sizeof(count));
for(int j=0;j<board[i].size();j++)
{
if(board[j][i]!='.')
{
count[board[j][i]-48]++;
if(count[board[j][i]-48]>1)
{
return false;
}
}
}
}
memset(count,0,sizeof(count));
for(int row_end=2;row_end<9;row_end+=3)
{
for(int col_end=2;col_end<9;col_end+=3)
{
memset(count,0,sizeof(count));
for(int i=row_end-2;i<=row_end;i++)
{
for(int j=col_end-2;j<=col_end;j++)
{
if(board[i][j]!='.')
{
count[board[i][j]-48]++;
if(count[board[i][j]-48]>1)
return false;
}
}
}
}
}
return true;
}
};