forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Naughton Jr
authored and
Kevin Naughton Jr
committed
May 29, 2018
1 parent
b08c50a
commit 4b06ffc
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx) | ||
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'. | ||
//A partially filled sudoku which is valid. | ||
|
||
//Note: | ||
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. | ||
|
||
class ValidSudoku { | ||
public boolean isValidSudoku(char[][] board) { | ||
for(int i = 0; i < board.length; i++){ | ||
HashSet<Character> rows = new HashSet<Character>(); | ||
HashSet<Character> columns = new HashSet<Character>(); | ||
HashSet<Character> box = new HashSet<Character>(); | ||
for (int j = 0; j < board[0].length; j++){ | ||
if(board[i][j] != '.' && !rows.add(board[i][j])) { | ||
return false; | ||
} | ||
if(board[j][i]!='.' && !columns.add(board[j][i])) { | ||
return false; | ||
} | ||
int rowIndex = (i / 3) * 3; | ||
int columnIndex = (i % 3) * 3; | ||
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx) | ||
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'. | ||
//A partially filled sudoku which is valid. | ||
|
||
//Note: | ||
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. | ||
|
||
class ValidSudoku { | ||
public boolean isValidSudoku(char[][] board) { | ||
for(int i = 0; i < board.length; i++){ | ||
HashSet<Character> rows = new HashSet<Character>(); | ||
HashSet<Character> columns = new HashSet<Character>(); | ||
HashSet<Character> box = new HashSet<Character>(); | ||
for (int j = 0; j < board[0].length; j++){ | ||
if(board[i][j] != '.' && !rows.add(board[i][j])) { | ||
return false; | ||
} | ||
if(board[j][i]!='.' && !columns.add(board[j][i])) { | ||
return false; | ||
} | ||
int rowIndex = (i / 3) * 3; | ||
int columnIndex = (i % 3) * 3; | ||
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx) | ||
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'. | ||
//A partially filled sudoku which is valid. | ||
|
||
//Note: | ||
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. | ||
|
||
class ValidSudoku { | ||
public boolean isValidSudoku(char[][] board) { | ||
for(int i = 0; i < board.length; i++){ | ||
HashSet<Character> rows = new HashSet<Character>(); | ||
HashSet<Character> columns = new HashSet<Character>(); | ||
HashSet<Character> box = new HashSet<Character>(); | ||
for (int j = 0; j < board[0].length; j++){ | ||
if(board[i][j] != '.' && !rows.add(board[i][j])) { | ||
return false; | ||
} | ||
if(board[j][i]!='.' && !columns.add(board[j][i])) { | ||
return false; | ||
} | ||
int rowIndex = (i / 3) * 3; | ||
int columnIndex = (i % 3) * 3; | ||
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx) | ||
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'. | ||
//A partially filled sudoku which is valid. | ||
|
||
//Note: | ||
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. | ||
|
||
class ValidSudoku { | ||
public boolean isValidSudoku(char[][] board) { | ||
for(int i = 0; i < board.length; i++){ | ||
HashSet<Character> rows = new HashSet<Character>(); | ||
HashSet<Character> columns = new HashSet<Character>(); | ||
HashSet<Character> box = new HashSet<Character>(); | ||
for (int j = 0; j < board[0].length; j++){ | ||
if(board[i][j] != '.' && !rows.add(board[i][j])) { | ||
return false; | ||
} | ||
if(board[j][i]!='.' && !columns.add(board[j][i])) { | ||
return false; | ||
} | ||
int rowIndex = (i / 3) * 3; | ||
int columnIndex = (i % 3) * 3; | ||
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|