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.