using
System;
using
System.Collections.Generic;
class
GFG {
public
static
bool
notInRow(
char
[, ] arr,
int
row)
{
HashSet<
char
> st =
new
HashSet<
char
>();
for
(
int
i = 0; i < 9; i++) {
if
(st.Contains(arr[row, i]))
return
false
;
if
(arr[row, i] !=
'.'
)
st.Add(arr[row, i]);
}
return
true
;
}
public
static
bool
notInCol(
char
[, ] arr,
int
col)
{
HashSet<
char
> st =
new
HashSet<
char
>();
for
(
int
i = 0; i < 9; i++) {
if
(st.Contains(arr[i, col]))
return
false
;
if
(arr[i, col] !=
'.'
)
st.Add(arr[i, col]);
}
return
true
;
}
public
static
bool
notInBox(
char
[, ] arr,
int
startRow,
int
startCol)
{
HashSet<
char
> st =
new
HashSet<
char
>();
for
(
int
row = 0; row < 3; row++) {
for
(
int
col = 0; col < 3; col++) {
char
curr
= arr[row + startRow, col + startCol];
if
(st.Contains(curr))
return
false
;
if
(curr !=
'.'
)
st.Add(curr);
}
}
return
true
;
}
public
static
bool
isValid(
char
[, ] arr,
int
row,
int
col)
{
return
notInRow(arr, row) && notInCol(arr, col)
&& notInBox(arr, row - row % 3, col - col % 3);
}
public
static
bool
isValidConfig(
char
[, ] arr,
int
n)
{
for
(
int
i = 0; i < n; i++) {
for
(
int
j = 0; j < n; j++) {
if
(!isValid(arr, i, j))
return
false
;
}
}
return
true
;
}
public
static
void
Main(
string
[] args)
{
char
[, ] board =
new
char
[, ] {
{
'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'
}
};
Console.WriteLine(
(isValidConfig(board, 9) ?
"YES"
:
"NO"
));
}
}