2014年11月27日星期四

[Leetcode] Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
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.

Simple implementation problem. For each position on the board (x, y), we check:
  1. the row x
  2. the column y
  3. the square where (x, y) located in, it is determined by (x/3)*3 ~ (x/3+1)*3 and (y/3)*3 ~(y/3+1)*3
The over all time complexity is O(n^3) and space complexity is O(1).

    public boolean isValidSudoku(char[][] board) {
        int m = board.length;
        int n = board[0].length;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(!check(board, i, j))
                    return false;
            }
        }
        return true;
    }
    
    private boolean check(char[][] board, int x, int y) {
        int m = board.length;
        int n = board[0].length;
        if(board[x][y] == '.')
            return true;
        // check row
        for(int i = 0; i < n; i++) {
            if(i != y && board[x][i] == board[x][y])
                return false;
        }
        // check column
        for(int i = 0; i < m; i++) {
            if(i != x && board[i][y] == board[x][y])
                return false;
        }
        // check 3*3 square
        for(int i = (x/3)*3; i < (x/3+1)*3; i++) {
            for(int j = (y/3)*3; j < (y/3+1)*3; j++) {
                if(i != x && j != y && board[i][j] == board[x][y])
                    return false;
            }
        }
        return true;
    }

Another method is to use a hashset or an array to hold the element we have meet in the row/column/square we are examining. This would reduce the time complexity to O(n^2) with space complexity to O(n).

public class Solution {
    public boolean isValidSudoku(char[][] board) {
        int m = board.length;
        int n = board[0].length;
        HashSet<Character> set = new HashSet<Character>();
        // check row
        for(int i = 0; i < m; i++) {
            set.clear();
            for(int j = 0; j < n; j++) {
                if(board[i][j] != '.') {
                    if(set.contains(board[i][j]) == true)
                        return false;
                    else
                        set.add(board[i][j]);
                }
            }
        }
        // check column
        for(int i = 0; i < n; i++) {
            set.clear();
            for(int j = 0; j < m; j++) {
                if(board[j][i] != '.') {
                    if(set.contains(board[j][i]) == true)
                        return false;
                    else
                        set.add(board[j][i]);
                }
            }
        }
        // check square
        for(int i = 0; i < m; i += 3) {
            for(int j = 0; j < n; j += 3) {
                set.clear();
                for(int x = (i/3)*3; x < (i/3+1)*3; x++) {
                    for(int y = (j/3)*3; y < (j/3+1)*3; y++) {
                        if(board[x][y] != '.') {
                            if(set.contains(board[x][y]) == true)
                                return false;
                            else
                                set.add(board[x][y]);
                        }
                    }
                }
            }
        }
        return true;
    }
}

没有评论:

发表评论