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;
}
Category >> Algorithms >> Interview >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Suraj Rukhaiyar

    getting ArrayIndexOutOfBoundsException at

    m[(int) (board[i][j] – ‘1’)]

  • Alexander Gryanko

    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;

    }

    }

  • Vimukthi Weerasiri

    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;
    }
    }