Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 36 · Matrix

Valid Sudoku

LeetCode 36 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

One set of descriptive keys for rows, columns and boxes.

நடுத்தரம்ArrayHash TableMatrix
LeetCode இல் திற ↗

நான் Valid Sudoku (LeetCode 36) ஐ இப்படித் தீர்த்தேன் — Matrix பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Each filled digit belongs to a row, a column and a 3×3 box. Encode each of those facts as a string like 'row 0 has 5'; if any fact was already recorded, the board is invalid.

Approach

  1. 1

    Scan every filled cell.

  2. 2

    Build keys for its row, column and box (i//3, j//3).

  3. 3

    If any key exists already, return False; otherwise add all three.

நான் எப்படித் தீர்த்தேன்

Set of keysPythonO(1) for a fixed 9×9 board
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        #check if a number exists in i th row, j th column, and 3*3 box - i//3,j//3
        seen=set()
        for i in range(9):
            for j in range(9):
                val = board[i][j]
                if val!=".":
                    number_in_row = f"row {i} has {val}" 
                    number_in_col = f"col {j} has {val}"
                    number_in_box = f"box {i//3,j//3} has {val}"
                    if number_in_row in seen or number_in_col in seen or number_in_box in seen:
                        return False
                    seen.add(number_in_row)
                    seen.add(number_in_col)
                    seen.add(number_in_box)
        return True


ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Valid Sudoku — அடிக்கடி கேட்கப்படும் கேள்விகள்

Valid Sudoku பின்னாலுள்ள intuition என்ன?

Each filled digit belongs to a row, a column and a 3×3 box. Encode each of those facts as a string like 'row 0 has 5'; if any fact was already recorded, the board is invalid.

Valid Sudoku (LeetCode 36) ஐ எப்படித் தீர்ப்பது?

1. Scan every filled cell. 2. Build keys for its row, column and box (i//3, j//3). 3. If any key exists already, return False; otherwise add all three.

Valid Sudoku இன் time மற்றும் space complexity என்ன?

Time: O(81). Space: O(81). (Accepted approach: Set of keys.)

Valid Sudoku இல் பொதுவான தவறுகள் என்ன?

Only check filled cells — the board doesn't need to be solvable.