Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added solution for Q36 - validate sudoku #22

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/main/java/leetcode/matrix/q36/ValidSudoku.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
package leetcode.matrix.q36;
public class ValidSudoku {
public boolean isValidSudoku(char[][] board) {
return false;

if (board == null || board.length == 0 || board[0].length == 0) {
return false;
}

if (board.length != 9 || board[0].length != 9) {
return false;
}

boolean[][] rows = new boolean[9][9];
boolean[][] cols = new boolean[9][9];
boolean[][] subgrids = new boolean[9][9];

for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
continue;
}
int num = board[i][j] - '1';
int subgridIndex = getSubgridIndex(i, j);
if (rows[i][num] || cols[j][num] || subgrids[subgridIndex][num]) {
return false;
}
rows[i][num] = true;
cols[j][num] = true;
subgrids[subgridIndex][num] = true;
}
}
return true;
}

private int getSubgridIndex(int i, int j) {
return ((i / 3) * 3) + (j / 3);
}
}
160 changes: 80 additions & 80 deletions src/test/java/leetcode/matrix/q36/ValidSudokuTests.java
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
//package leetcode.matrix.q36;
//
//import static org.junit.jupiter.api.Assertions.*;
//import org.junit.jupiter.api.Test;
//import leetcode.matrix.q36.ValidSudoku;
//
//public class ValidSudokuTests {
//
// // Test case for a valid sudoku board
// @Test
// public void testValidSudoku() {
// char[][] board = {
// {'5', '3', '.', '.', '7', '.', '.', '.', '.'},
// {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
// {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
// {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
// {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
// {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
// {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
// {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
// {'.', '.', '.', '.', '8', '.', '.', '7', '9'}
// };
// ValidSudoku ob = new ValidSudoku();
// assertTrue(ob.isValidSudoku(board));
// }
//
// // Test case for invalid sudoku with duplicate values in a row
// @Test
// public void testInvalidRow() {
// char[][] board = {
// {'5', '3', '3', '.', '7', '.', '.', '.', '.'},
// {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
// {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
// {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
// {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
// {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
// {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
// {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
// {'.', '.', '.', '.', '8', '.', '.', '7', '9'}
// };
// ValidSudoku ob = new ValidSudoku();
// assertFalse(ob.isValidSudoku(board));
// }
//
// // Test case for invalid sudoku with duplicate values in a column
// @Test
// public void testInvalidColumn() {
// char[][] board = {
// {'5', '3', '.', '.', '7', '.', '.', '.', '.'},
// {'3', '.', '.', '1', '9', '5', '.', '.', '.'},
// {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
// {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
// {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
// {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
// {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
// {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
// {'.', '.', '.', '.', '8', '.', '.', '7', '9'}
// };
// ValidSudoku ob = new ValidSudoku();
// assertFalse(ob.isValidSudoku(board));
// }
//
// // Test case for invalid sudoku with duplicate values in a 3x3 subgrid
// @Test
// public void testInvalidSubgrid() {
// char[][] board = {
// {'5', '3', '.', '.', '7', '.', '.', '.', '.'},
// {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
// {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
// {'8', '.', '.', '.', '6', '3', '.', '.', '3'},
// {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
// {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
// {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
// {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
// {'.', '.', '.', '.', '8', '.', '.', '7', '9'}
// };
// ValidSudoku ob = new ValidSudoku();
// assertFalse(ob.isValidSudoku(board));
// }
//}
package leetcode.matrix.q36;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import leetcode.matrix.q36.ValidSudoku;

public class ValidSudokuTests {

// Test case for a valid sudoku board
@Test
public void testValidSudoku() {
char[][] board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
};
ValidSudoku ob = new ValidSudoku();
assertTrue(ob.isValidSudoku(board));
}

// Test case for invalid sudoku with duplicate values in a row
@Test
public void testInvalidRow() {
char[][] board = {
{'5', '3', '3', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
};
ValidSudoku ob = new ValidSudoku();
assertFalse(ob.isValidSudoku(board));
}

// Test case for invalid sudoku with duplicate values in a column
@Test
public void testInvalidColumn() {
char[][] board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '3', '.', '.', '8', '.', '.', '7', '9'}
};
ValidSudoku ob = new ValidSudoku();
assertFalse(ob.isValidSudoku(board));
}

// Test case for invalid sudoku with duplicate values in a 3x3 subgrid
@Test
public void testInvalidSubgrid() {
char[][] board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '1', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '3', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
};
ValidSudoku ob = new ValidSudoku();
assertFalse(ob.isValidSudoku(board));
}
}
Loading