- 문제 링크: https://leetcode.com/problems/valid-sudoku/

- 난이도: Medium

 

걍.. 노가다

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        # rule 1
        for row in range(9):
            numbers = set()
            for col in range(9):
                cell = board[row][col]
                if cell == ".":
                    continue
                if cell in numbers:
                    return False
                numbers.add(cell)

        # rule 2
        for col in range(9):
            numbers = set()
            for row in range(9):
                cell = board[row][col]
                if cell == ".":
                    continue
                if cell in numbers:
                    return False
                numbers.add(cell)

        # rule 3
        for sub_row in range(3):
            for sub_col in range(3):
                numbers = set()
                for i in range(9):
                    row = sub_row*3 + (i // 3)
                    col = sub_col*3 + (i % 3)

                    cell = board[row][col]
                    if cell == ".":
                        continue
                    if cell in numbers:
                        return False
                    numbers.add(cell)

        return True
반응형