From bb24e260fa4fa116113e38de78f2a5c825d800d2 Mon Sep 17 00:00:00 2001 From: Shinjanee Gupta Date: Thu, 12 Mar 2026 11:27:20 -0700 Subject: [PATCH] backtracking-3 --- NQueens.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ WordSearch.py | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 NQueens.py create mode 100644 WordSearch.py diff --git a/NQueens.py b/NQueens.py new file mode 100644 index 00000000..b0c7512a --- /dev/null +++ b/NQueens.py @@ -0,0 +1,61 @@ +# Time Complexity : O(n * n!) +# Space Complexity : O( n^2 ) for the board + O( n ) recursive stack space +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : We’re placing queens row by row and checking if each placement is safe. +# If it's valid, we place it and move to the next row recursively. +# When all rows are filled, we convert the board to a list and store it in the result. + +class Solution: + def solveNQueens(self, n: int) -> List[List[str]]: + self.res = [] + board = [[False]*n for _ in range(n)] + self.helper(board, 0, n) + return self.res + + def helper(self, board, row, n): + if row == n: + temp = [] + for i in range(n): + sb = [] + for j in range(n): + if board[i][j]: + sb.append("Q") + else: + sb.append(".") + temp.append("".join(sb)) + self.res.append(temp) + return + + for j in range(n): + if self.isValid(board, row, j, n): + board[row][j] = True + self.helper(board, row+1, n) + board[row][j] = False + + def isValid(self, board, i, j, n): + r,c = i, j + while r >= 0: + if board[r][c]: + return False + r -= 1 + + r,c = i, j + while r >= 0 and c >= 0: + if board[r][c]: + return False + r -= 1 + c -= 1 + + r,c = i, j + while r >= 0 and c < n: + if board[r][c]: + return False + r -= 1 + c += 1 + + return True + + + + \ No newline at end of file diff --git a/WordSearch.py b/WordSearch.py new file mode 100644 index 00000000..6c2999c0 --- /dev/null +++ b/WordSearch.py @@ -0,0 +1,44 @@ +# Time Complexity : O(m * n * 4^L) where L = word.length() +# Space Complexity : O(L) recursion stack in the worst case. +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No +# Approach : We go cell by cell and start DFS if the first character matches. +# We mark visited cells as # to avoid reuse during the same path. +# If we match all characters, we return true, else we backtrack. + +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + self.dirs = [[0,-1], [0, 1], [-1, 0], [1,0]] + self.rows, self.cols = len(board), len(board[0]) + + for i in range(self.rows): + for j in range(self.cols): + if self.dfs(board, i, j, word, 0): + return True + return False + + + def dfs(self, board: List[List[str]], i: int, j: int, word: str, idx: int) -> bool: + if idx == len(word): + return True + + if i < 0 or j < 0 or i == self.rows or j == self.cols or board[i][j] == '#': + return False + + if board[i][j] != word[idx]: + return False + + board[i][j] = "#" + + for dx, dy in self.dirs: + r = dx + i + c = dy + j + + if self.dfs(board, r, c, word, idx + 1): + return True + + board[i][j] = word[idx] + + return False + + \ No newline at end of file