Simple clean solution for Valid Sudoku

1. check for valid input parameters
2. check each 3x3 grid
3. check the rows
4. check the columns
``````public class Solution
{
public bool IsValidSudoku(char[,] board)
{

if (board == null)
{
return false;
}

if (board.GetLength(0) != board.GetLength(1))
{
return false;
}

if (board.GetLength(0) != 9)
{
return false;
}

var boardLength = board.GetLength(0);
for (int i = 0; i < boardLength; i = i + 3)
{
for (int j = 0; j < boardLength; j += 3)
{
if(!IsGridValid(board, i, j))
{
return false;
}
}
}

for (int i = 0; i < boardLength; i++)
{
if (!IsRowValid(board, i))
{
return false;
}
}

for (int i = 0; i < boardLength; i++)
{
if (!IsColumnValid(board, i))
{
return false;
}
}

return true;
}

private bool IsGridValid(char[,] board, int rowStartIndex, int columnStartIndex)
{
bool[] row = new bool[board.GetLength(0) + 1];

for (int i = rowStartIndex; i <= rowStartIndex + 2; i++)
{
for (int j = columnStartIndex; j <= columnStartIndex + 2; j++)
{
char boardNumber = board[i, j];
if (boardNumber == '.')
{
continue;
}

int boardNumberInt = int.Parse(boardNumber.ToString());
if (row[boardNumberInt] == true)
{
return false;
}

row[boardNumberInt] = true;
}
}

return true;
}

private bool IsRowValid(char[,] board, int rowNumber)
{
bool[] row = new bool[board.Length + 1];
if (board.Length <= rowNumber)
{
return false;
}

for (int i = 0; i < board.GetLength(0); i++)
{
char boardNumber = board[rowNumber, i];
if (boardNumber == '.')
{
continue;
}

int boardNumberInt = int.Parse(boardNumber.ToString());
if (row[boardNumberInt] == true)
{
return false;
}

row[boardNumberInt] = true;
}

return true;
}

private bool IsColumnValid(char[,] board, int columnNumber)
{
bool[] column = new bool[board.GetLength(0) + 1];
if (board.GetLength(0) <= columnNumber)
{
return false;
}

for (int i = 0; i < board.GetLength(0); i++)
{
var boardNumber = board[i, columnNumber];
if (boardNumber == '.')
{
continue;
}

int boardNumberInt = int.Parse(boardNumber.ToString());
if (column[boardNumberInt] == true)
{
return false;
}

column[boardNumberInt] = true;
}

return true;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.