1. Backtracking (Brute Force Validation)

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        def isValid(r, c, ch):
            for i in range(9):
                if board[r][i] == ch or board[i][c] == ch:
                    return False
                if board[3 * (r // 3) + i // 3][3 * (c // 3) + i % 3] == ch:
                    return False
            return True

        def dfs(r, c):
            if r == 9:
                return True
            if c == 9:
                return dfs(r + 1, 0)
            if board[r][c] != ".":
                return dfs(r, c + 1)

            for ch in "123456789":
                if isValid(r, c, ch):
                    board[r][c] = ch
                    if dfs(r, c + 1):
                        return True
                    board[r][c] = "."
            return False

        dfs(0, 0)

Time & Space Complexity

  • Time complexity: O(9m∗27)O(9 ^ m * 27)
  • Space complexity: O(m)O(m) for recursion stack.

Where mm is the number of empty cells in the board.


2. Backtracking (Hash Set)

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        rows = [set() for _ in range(9)]
        cols = [set() for _ in range(9)]
        boxes = [set() for _ in range(9)]
        empty = []

        for r in range(9):
            for c in range(9):
                if board[r][c] == ".":
                    empty.append((r, c))
                else:
                    ch = board[r][c]
                    rows[r].add(ch)
                    cols[c].add(ch)
                    boxes[(r // 3) * 3 + c // 3].add(ch)

        def dfs(i):
            if i == len(empty):
                return True

            r, c = empty[i]
            b = (r // 3) * 3 + c // 3
            for ch in "123456789":
                if ch in rows[r] or ch in cols[c] or ch in boxes[b]:
                    continue
                board[r][c] = ch
                rows[r].add(ch)
                cols[c].add(ch)
                boxes[b].add(ch)
                if dfs(i + 1):
                    return True
                rows[r].remove(ch)
                cols[c].remove(ch)
                boxes[b].remove(ch)
            board[r][c] = "."
            return False

        dfs(0)

Time & Space Complexity

  • Time complexity: O(9m)O(9 ^ m)
  • Space complexity: O(m)O(m)

Where mm is the number of empty cells in the board.


3. Backtracking (Bit Mask)

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        rows = [0] * 9
        cols = [0] * 9
        boxes = [0] * 9
        empty = []

        for r in range(9):
            for c in range(9):
                if board[r][c] == ".":
                    empty.append((r, c))
                else:
                    bit = 1 << (int(board[r][c]) - 1)
                    rows[r] |= bit
                    cols[c] |= bit
                    boxes[(r // 3) * 3 + c // 3] |= bit

        def dfs(i):
            if i == len(empty):
                return True

            r, c = empty[i]
            b = (r // 3) * 3 + c // 3
            used = rows[r] | cols[c] | boxes[b]
            for d in range(1, 10):
                bit = 1 << (d - 1)
                if used & bit:
                    continue
                board[r][c] = str(d)
                rows[r] |= bit
                cols[c] |= bit
                boxes[b] |= bit
                if dfs(i + 1):
                    return True
                rows[r] ^= bit
                cols[c] ^= bit
                boxes[b] ^= bit
            board[r][c] = "."
            return False

        dfs(0)

Time & Space Complexity

  • Time complexity: O(9m)O(9 ^ m)
  • Space complexity: O(m)O(m)

Where mm is the number of empty cells in the board.


4. Backtracking (Bit Mask + Most Constrained Cell)

class Solution:
    def solveSudoku(self, board: List[List[str]]) -> None:
        rows = [0] * 9
        cols = [0] * 9
        boxes = [0] * 9
        empty = []

        for r in range(9):
            for c in range(9):
                if board[r][c] == ".":
                    empty.append((r, c))
                else:
                    bit = 1 << (int(board[r][c]) - 1)
                    rows[r] |= bit
                    cols[c] |= bit
                    boxes[(r // 3) * 3 + c // 3] |= bit

        def dfs(i):
            if i == len(empty):
                return True

            best, bestCnt = i, 10
            for j in range(i, len(empty)):
                r, c = empty[j]
                used = rows[r] | cols[c] | boxes[(r // 3) * 3 + c // 3]
                cnt = 9 - bin(used).count("1")
                if cnt < bestCnt:
                    best, bestCnt = j, cnt
                    if cnt <= 1:
                        break
            empty[i], empty[best] = empty[best], empty[i]

            r, c = empty[i]
            b = (r // 3) * 3 + c // 3
            used = rows[r] | cols[c] | boxes[b]
            for d in range(1, 10):
                bit = 1 << (d - 1)
                if used & bit:
                    continue
                board[r][c] = str(d)
                rows[r] |= bit
                cols[c] |= bit
                boxes[b] |= bit
                if dfs(i + 1):
                    return True
                rows[r] ^= bit
                cols[c] ^= bit
                boxes[b] ^= bit
            board[r][c] = "."
            return False

        dfs(0)

Time & Space Complexity

  • Time complexity: O(m∗9m)O(m * 9 ^ m) in the worst case, but choosing the most constrained cell first prunes the search tree drastically in practice.
  • Space complexity: O(m)O(m)

Where mm is the number of empty cells in the board.