-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid_sudoku.cpp
101 lines (92 loc) · 3.02 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
// Given a board, determine if it has a valid sudoku. The board has valid sudoku if
// Each row has 1-9 without repitition
// Each column has 1-9 without repitition
// Each of the 3*3 sub grids has 1-9 wihtout repitition
// Example Input -
// [["5","3",".",".","7",".",".",".","."]
// ,["6",".",".","1","9","5",".",".","."]
// ,[".","9","8",".",".",".",".","6","."]
// ,["8",".",".",".","6",".",".",".","3"]
// ,["4",".",".","8",".","3",".",".","1"]
// ,["7",".",".",".","2",".",".",".","6"]
// ,[".","6",".",".",".",".","2","8","."]
// ,[".",".",".","4","1","9",".",".","5"]
// ,[".",".",".",".","8",".",".","7","9"]]
// Expected output - True.
#include <iostream>
#include <unordered_map>
bool isValidSudoku(vector<vector<char>>& board)
{
std::unordered_map<char,int> fmap;
for(int i=0; i<board.size(); i++)
{
for(int j=0; j<board[0].size(); j++)
{
if(board[i][j] != '.')
{
if(!fmap[board[i][j]])
fmap[board[i][j]]++;
else
return false;
}
}
fmap.clear();
}
for(int j=0; j<board.size(); j++)
{
for(int i=0; i<board[0].size(); i++)
{
if(board[i][j] != '.')
{
if(!fmap[board[i][j]])
fmap[board[i][j]]++;
else
return false;
}
}
fmap.clear();
}
int i=0;
while(i<board.size())
{
int j=0;
while(j<board[0].size())
{
int m = i;
while(m < i+3)
{
int k = j;
while(k<j+3)
{
if(board[m][k] != '.')
{
if(!fmap[board[m][k]])
fmap[board[m][k]]++;
else
return false;
}
k++;
}
m++;
}
fmap.clear();
j += 3;
}
i += 3;
}
return true;
}
int main()
{
vector<vector<char>> board {{"5","3",".",".","7",".",".",".","."}
,{"6",".",".","1","9","5",".",".","."}
,[".","9","8",".",".",".",".","6","."}
,{"8",".",".",".","6",".",".",".","3"}
,{"4",".",".","8",".","3",".",".","1"}
,{"7",".",".",".","2",".",".",".","6"}
,{".","6",".",".",".",".","2","8","."}
,{".",".",".","4","1","9",".",".","5"}
,{".",".",".",".","8",".",".","7","9"}};
std::cout << isValidSudoku(board) << std::endl;
return 0;
}