From c69907ff4bde6f40060c4ddb9bf4751004f80f3a Mon Sep 17 00:00:00 2001 From: Param Desai Date: Tue, 30 Sep 2025 10:57:50 -0700 Subject: [PATCH] Backtracking 3 Done --- nqueens.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ word.py | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 nqueens.py create mode 100644 word.py diff --git a/nqueens.py b/nqueens.py new file mode 100644 index 00000000..9880d03c --- /dev/null +++ b/nqueens.py @@ -0,0 +1,65 @@ +# Time Complexity : O(N!) where N is the size of the board +# Space Complexity : O(N^2) where N is the size of the board +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach: +# I am makeing a boolean grid to keep track of the queens placed on the board. +# I am using backtracking to place the queens on the board row by row. +# For each row, I am iterating through each column and checking if it is safe to place a queen there. +# If it is safe, I place the queen and call the backtrack function recursively for the next row. +# If I reach the end of the board, I add the current configuration to the result list. +# If it is not safe, I backtrack and try the next column. +# Finally, I return the result list. + + +from typing import List +class Solution: + def solveNQueens(self, n: int) -> List[List[str]]: + grid = [[False]*n for _ in range(n)] + result = [] + def backtrack(grid,r): + if r == n: + li = [] + for i in range(n): + sb = [] + for j in range(n): + if grid[i][j]: + sb.append("Q") + else: + sb.append(".") + li.append("".join(sb)) + result.append(li) + return + + for c in range(n): + if isSafe(grid,r,c): + grid[r][c] = True + backtrack(grid,r+1) + grid[r][c] = False + def isSafe(grid,r,c): + i = r + j = c + while i >=0: + if grid[i][j]: + return False + i -= 1 + i = r + j = c + while i >= 0 and j >= 0: + if grid[i][j]: + return False + i -= 1 + j -= 1 + i = r + j = c + while i >= 0 and j < n : + if grid[i][j] : + return False + i -= 1 + j += 1 + return True + backtrack(grid,0) + return result + + \ No newline at end of file diff --git a/word.py b/word.py new file mode 100644 index 00000000..e355003f --- /dev/null +++ b/word.py @@ -0,0 +1,44 @@ +# Time Complexity : O(M*N*4^L) where M is the length of the word, N is the size of the board and L is the length of the word +# Space Complexity : O(L) where L is the length of the word +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : No + +# Your code here along with comments explaining your approach: +# I am using DFS to traverse the board starting from each cell. +# I am keeping track of the current index in the word and the current cell in the board. +# If the current index is equal to the length of the word, I return True. +# If the current cell is out of bounds or the character in the cell is not equal to the character in the word at the current index, I return False. +# I am marking the current cell as visited by changing its value to "#". +# I am then recursively calling the DFS function for the 4 adjacent cells (up, down, left, right) and incrementing the index in the word. +# If any of the recursive calls return True, I return True. +# After exploring all 4 directions, I backtrack by restoring the original value of the cell. +# Finally, if no starting cell leads to a successful match, I return False. + + +from typing import List +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + m = len(board) + n = len(board[0]) + dirs = [(1,0),(0,1),(0,-1),(-1,0)] + def dfs(i,j,idx): + if idx == len(word): + return True + if i < 0 or j < 0 or i == m or j == n or board[i][j] == "#": + return False + if board[i][j] != word[idx]: + return False + board[i][j] = "#" + for dr,dc in dirs: + nr = dr + i + nc = dc + j + if dfs(nr,nc,idx+1): + return True + board[i][j] = word[idx] + for i in range(m): + for j in range(n): + if dfs(i,j,0): + return True + return False + + \ No newline at end of file