# Sudoku validator

If you’re looking for a coding job and haven’t started practicing for interview coding challenges, I’d highly recommend checking out codefights.  It’s also a good place just to have fun and keep your mind sharp.  It really is addicting.  Until now I’ve been using a site called codechef.  Codechef is great but I like the content and organization of codefights a bit better.  I don’t think I’ll share all of my solutions for codefights on here but I’d like to share a few that I really had fun with.  Here’s my solution for a sudoku validator:

```bool sudoku2(char[][] grid)
{
string[] subgrids = new string;
for(int i = 0; i < 9; i++)
{
string row = "";
string column = "";
for(int j = 0; j < 9; j++)
{
//Check rows
if(grid[i][j] != '.' && row.Contains(grid[i][j]))return false;
row = row + grid[i][j];
//Check columns
if(grid[j][i] != '.' && column.Contains(grid[j][i]))return false;
column = column + grid[j][i];
//Check subgrids
if(i < 3 && j < 3)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(i < 3 && 2 < j && j < 6)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(i < 3 && j > 5)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(2 < i && i < 6 && j < 3)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(2 < i  && i< 6 && 2 < j  && j < 3)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(2 < i && i < 6 && j > 5)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(i > 5 && j < 3)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(i > 5 && 2 < j  && j < 6)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
else if(i > 5 && j > 5)
{
if(!String.IsNullOrEmpty(subgrids))
{
if(grid[i][j] != '.' && subgrids.Contains(grid[i][j]))return false;
}
subgrids = subgrids + grid[i][j];
}
}
}

return true;
}
```

If you haven’t done this on codefights you might notice that this validator allows for blank spaces.  The blank spaces are identified with periods.

In an actual game of sudoku you probably would not want to validate a puzzle until the end but something like this might be useful as a sudoku trainer.  For example you could run this function every time a player makes a move to let them know if it is a valid move or not.  The only problem with that though is that it would allow a player to make an incorrect move early on which wouldn’t be very helpful.  If I were to design a sudoku trainer, I would just check the player’s input with the completed table.  That would also be far fewer lines of code.  But I guess the purpose of these challenges are just to see if you can solve the problem, not to actually write useful code for an application.¯\_(ツ)_/¯