36. 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.

Solution: HashSet

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        seen = set()
        for row in xrange(9):
            for col in xrange(9):
                num = board[row][col]
                if num == '.':
                    continue
                for val in (row, -1, num), (-1, col, num), (row / 3, col / 3, num):
                    if val in seen:
                        return False
                    seen.add(val)
        return True

results matching ""

    No results matching ""