Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions nqueens.py
Original file line number Diff line number Diff line change
@@ -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


44 changes: 44 additions & 0 deletions word.py
Original file line number Diff line number Diff line change
@@ -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