Skip to content

Commit

Permalink
add valid sudoku
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Naughton Jr authored and Kevin Naughton Jr committed May 29, 2018
1 parent b08c50a commit 4b06ffc
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
31 changes: 31 additions & 0 deletions company/apple/ValidSudoku.java
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;
}
}

31 changes: 31 additions & 0 deletions company/snapchat/ValidSudoku.java
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;
}
}

30 changes: 30 additions & 0 deletions company/uber/ValidSudoku.java
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;
}
}
31 changes: 31 additions & 0 deletions leetcode/hash-table/ValidSudoku.java
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;
}
}

0 comments on commit 4b06ffc

Please sign in to comment.