Skip to content
LC-0036 Medium LeetCode

36. Valid Sudoku

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 62% Topics: Array, Hash Table, Matrix
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(9^2)
# Space: O(9)

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        for i in xrange(9):
            if not self.isValidList([board[i][j] for j in xrange(9)]) or \
               not self.isValidList([board[j][i] for j in xrange(9)]):
                return False
        for i in xrange(3):
            for j in xrange(3):
                if not self.isValidList([board[m][n] for n in xrange(3 * j, 3 * j + 3) \
                                                     for m in xrange(3 * i, 3 * i + 3)]):
                    return False
        return True

    def isValidList(self, xs):
        xs = filter(lambda x: x != '.', xs)
        return len(set(xs)) == len(xs)


Solution from kamyu104/LeetCode-Solutions · MIT