Python solution using set


  • 0
    W

    use integer array for rows and cols, but set to check square. set is stored as (row, col, value) so that it is globally unique.
    '''

    def isValidSudoku(self, board):      
        s = set()
        for i in range(9):
            int_arr = [0] * 9
            int_arr1 = [0] * 9
            for j in range(9):
                if board[i][j] != '.':
                    if int_arr[int(board[i][j])-1] == 1:
                        return False
                    else:
                        int_arr[int(board[i][j])-1] = 1
                    if (i//3, j//3, int(board[i][j])) in s:
                        return False
                    else:
                        s.add((i//3,j//3,int(board[i][j])))
                if board[j][i] != '.':
                    if int_arr1[int(board[j][i])-1] == 1:
                        return False
                    else:
                        int_arr1[int(board[j][i])-1] = 1
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.