Python short solution


#1
def isValidSudoku(self, A):
    row, col, sec = {}, {}, {}
    for i in range(len(A)):
        row[i], col[i], sec[i] = set(), set(), set()
    for i in range(len(A)):
        for j in range(len(A)):
            if A[i][j] != '.':
                s = (i // 3) + 3 * (j // 3)
                if A[i][j] in row[i] or A[i][j] in col[j] or A[i][j] in sec[s]:
                    return 0
                row[i].add(A[i][j])
                col[j].add(A[i][j])
                sec[s].add(A[i][j])
    return 1

#2

pretty slick; here’s my version.

class Solution:
    def isValidSudoku(self, A):
        rows = [set() for _ in range(len(A))]
        cols = [set() for _ in range(len(A))]
        grid = [set() for _ in range(len(A))]

        for i in range(len(A)):
            for j in range(len(A)):
                current = A[i][j]
                if current != '.':
                    size = (i // 3) * 3 + j // 3
                    if current in rows[i] or current in cols[j] or current in grid[size]:
                        return 0
                    rows[i].add(current)
                    cols[j].add(current)
                    grid[size].add(current)
        return 1