Skip to content

Commit f1fdb58

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
add valid sudoku
1 parent c2b18ee commit f1fdb58

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

company/apple/ValidSudoku.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2+
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3+
//A partially filled sudoku which is valid.
4+
5+
//Note:
6+
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7+
8+
class ValidSudoku {
9+
public boolean isValidSudoku(char[][] board) {
10+
for(int i = 0; i < board.length; i++){
11+
HashSet<Character> rows = new HashSet<Character>();
12+
HashSet<Character> columns = new HashSet<Character>();
13+
HashSet<Character> box = new HashSet<Character>();
14+
for (int j = 0; j < board[0].length; j++){
15+
if(board[i][j] != '.' && !rows.add(board[i][j])) {
16+
return false;
17+
}
18+
if(board[j][i]!='.' && !columns.add(board[j][i])) {
19+
return false;
20+
}
21+
int rowIndex = (i / 3) * 3;
22+
int columnIndex = (i % 3) * 3;
23+
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) {
24+
return false;
25+
}
26+
}
27+
}
28+
return true;
29+
}
30+
}
31+

company/snapchat/ValidSudoku.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2+
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3+
//A partially filled sudoku which is valid.
4+
5+
//Note:
6+
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7+
8+
class ValidSudoku {
9+
public boolean isValidSudoku(char[][] board) {
10+
for(int i = 0; i < board.length; i++){
11+
HashSet<Character> rows = new HashSet<Character>();
12+
HashSet<Character> columns = new HashSet<Character>();
13+
HashSet<Character> box = new HashSet<Character>();
14+
for (int j = 0; j < board[0].length; j++){
15+
if(board[i][j] != '.' && !rows.add(board[i][j])) {
16+
return false;
17+
}
18+
if(board[j][i]!='.' && !columns.add(board[j][i])) {
19+
return false;
20+
}
21+
int rowIndex = (i / 3) * 3;
22+
int columnIndex = (i % 3) * 3;
23+
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) {
24+
return false;
25+
}
26+
}
27+
}
28+
return true;
29+
}
30+
}
31+

company/uber/ValidSudoku.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2+
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3+
//A partially filled sudoku which is valid.
4+
5+
//Note:
6+
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7+
8+
class ValidSudoku {
9+
public boolean isValidSudoku(char[][] board) {
10+
for(int i = 0; i < board.length; i++){
11+
HashSet<Character> rows = new HashSet<Character>();
12+
HashSet<Character> columns = new HashSet<Character>();
13+
HashSet<Character> box = new HashSet<Character>();
14+
for (int j = 0; j < board[0].length; j++){
15+
if(board[i][j] != '.' && !rows.add(board[i][j])) {
16+
return false;
17+
}
18+
if(board[j][i]!='.' && !columns.add(board[j][i])) {
19+
return false;
20+
}
21+
int rowIndex = (i / 3) * 3;
22+
int columnIndex = (i % 3) * 3;
23+
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) {
24+
return false;
25+
}
26+
}
27+
}
28+
return true;
29+
}
30+
}

leetcode/hash-table/ValidSudoku.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. (http://sudoku.com.au/TheRules.aspx)
2+
//The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
3+
//A partially filled sudoku which is valid.
4+
5+
//Note:
6+
//A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
7+
8+
class ValidSudoku {
9+
public boolean isValidSudoku(char[][] board) {
10+
for(int i = 0; i < board.length; i++){
11+
HashSet<Character> rows = new HashSet<Character>();
12+
HashSet<Character> columns = new HashSet<Character>();
13+
HashSet<Character> box = new HashSet<Character>();
14+
for (int j = 0; j < board[0].length; j++){
15+
if(board[i][j] != '.' && !rows.add(board[i][j])) {
16+
return false;
17+
}
18+
if(board[j][i]!='.' && !columns.add(board[j][i])) {
19+
return false;
20+
}
21+
int rowIndex = (i / 3) * 3;
22+
int columnIndex = (i % 3) * 3;
23+
if(board[rowIndex + j / 3][columnIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][columnIndex + j % 3])) {
24+
return false;
25+
}
26+
}
27+
}
28+
return true;
29+
}
30+
}
31+

0 commit comments

Comments
 (0)