LeetCode – Valid Sudoku (Java)

Determine if a Sudoku is valid. The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.

valid-sudoku-leetcode-java

Java Solution

public boolean isValidSudoku(char[][] board) {
	if (board == null || board.length != 9 || board[0].length != 9)
		return false;
	// check each column
	for (int i = 0; i < 9; i++) {
		boolean[] m = new boolean[9];
		for (int j = 0; j < 9; j++) {
			if (board[i][j] != '.') {
				if (m[(int) (board[i][j] - '1')]) {
					return false;
				}
				m[(int) (board[i][j] - '1')] = true;
			}
		}
	}
 
	//check each row
	for (int j = 0; j < 9; j++) {
		boolean[] m = new boolean[9];
		for (int i = 0; i < 9; i++) {
			if (board[i][j] != '.') {
				if (m[(int) (board[i][j] - '1')]) {
					return false;
				}
				m[(int) (board[i][j] - '1')] = true;
			}
		}
	}
 
	//check each 3*3 matrix
	for (int block = 0; block < 9; block++) {
		boolean[] m = new boolean[9];
		for (int i = block / 3 * 3; i < block / 3 * 3 + 3; i++) {
			for (int j = block % 3 * 3; j < block % 3 * 3 + 3; j++) {
				if (board[i][j] != '.') {
					if (m[(int) (board[i][j] - '1')]) {
						return false;
					}
					m[(int) (board[i][j] - '1')] = true;
				}
			}
		}
	}
 
	return true;
}

5 thoughts on “LeetCode – Valid Sudoku (Java)”

  1. The given solution is incorrect. It can return true for unsolvable sudoku. For example, a sudoku with the following first three lines:


    1 2 3 . . . . . .
    . . . . 7 . . . .
    4 5 6 . . . . . .

  2. What if we check the sum of all the digits in each row,column & squares. Assumption here is that board is filled all int 0 to 9. 0 means blank.

    public static boolean isValid(int board[][]) {
    boolean result = false;
    // check columns
    for (int i = 0; i < 9; i++) {
    int sum = 0;
    for (int j = 0; j < 9; j++) {
    sum += board[j][i];
    }
    if (sum == 45) {
    result = true;
    } else {
    return false;
    }
    }
    // check rows
    for (int i = 0; i < 9; i++) {
    int sum = 0;
    for (int j = 0; j < 9; j++) {
    sum += board[i][j];
    }
    if (sum == 45) {
    result = true;
    } else {
    return false;
    }
    }
    // check squares
    for (int i = 0; i < 9; i += 3) {
    for (int j = 0; j < 9; j += 3) {
    int sum = 0;
    for (int ii = i; ii < i + 3; ii++) {
    for (int jj = j; jj < j + 3; jj++) {
    sum += board[ii][jj];
    }
    }
    if (sum == 45) {
    result = true;
    } else {
    return false;
    }
    }
    }
    return result;
    }

  3. May be someone can help me with a better hash function for squares?

    public class Solution {

    public boolean isValidSudoku(char[][] board) {

    int[] row = new int[9];

    int[] col = new int[9];

    int[] sqr = new int[9];

    for (int zzz = 0; zzz < 9; zzz++) {

    row[zzz] = 0;

    col[zzz] = 0;

    sqr[zzz] = 0;

    }

    for (int i = 0; i < 9; i++) {

    for (int j = 0; j < 9; j++) {

    int k = hsh(i, j);

    if (board[i][j] != '.') {

    System.out.println(board[i][j]);

    int c = 1 <= 0 && i = 0 && j = 3 && j = 6 && j = 3 && i = 0 && j = 3 && j = 6 && j = 6 && i = 0 && j = 3 && j = 6 && j <= 8)

    return 8;

    }

    return -1;

    }

    }

  4. Using a HashSet for convenience.

    public class Solution {
    public boolean isValidSudoku(char[][] board) {
    // checking rows and columns
    HashSet set, set2;
    for (int i = 0; i < 9; i++) {
    set = new HashSet();
    set2 = new HashSet();
    for (int j = 0; j < 9; j++) {
    if(!set.add(board[i][j]) && board[i][j] != '.') return false;
    if(!set2.add(board[j][i]) && board[j][i] != '.') return false;
    }
    }
    // checking 9 boxes
    for (int x = 0; x < 9; x += 3) {
    for (int y = 0; y < 9; y += 3) {
    set = new HashSet();
    for (int i = x; i < x + 3; i++) {
    for (int j = y; j < y + 3; j++) {
    if(!set.add(board[i][j]) && board[i][j] != '.') return false;
    }
    }
    }
    }
    return true;
    }
    }

Leave a Comment