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
73 changes: 73 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Time Complexity : O(n * n!)
// Space Complexity : O(n^2) for the matrix + O(n) recursive stack
// 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
//We recurse through every row and check if its safe to place the queen if yes we recursivley check for other rows and backtrack them
//When placing a queen in particular row we check for top colums in prev rows, also check diagonally top left and top right
//When all the rows are filled we check if the particular cell has a queen we convert and place it in a string as Q else as "."
class Solution {
public List<List<String>> solveNQueens(int n) {
boolean [][] grid = new boolean[n][n];
List<List<String>> res = new ArrayList<>();
backtrack(grid, 0, n, res);
return res;
}

private void backtrack(boolean [][] grid , int r, int n, List<List<String>> res) {
//base
if (r == n) {// covered all rows
//place the queens in list
List<String> li = new ArrayList<>();
for(int i = 0; i<n; i++) {
StringBuilder sb = new StringBuilder();
for(int j = 0; j < n; j++) {
if(grid[i][j]) {
sb.append("Q");
} else {
sb.append(".");
}
}
li.add(sb.toString());
}
res.add(li);
}


//logic
for(int c=0; c<n ; c++) { //iterate over each row to check if its safe to place the queen
if(isSafe(grid, r, c, n)) {
//action
grid[r][c] = true;
//recurse
backtrack(grid, r+1, n, res);
//backtrack
grid[r][c] = false;
}
}
}

private boolean isSafe(boolean[][] grid, int r, int c, int n) {
//column up
for(int i = 0; i < r; i++) {
if(grid[i][c]) return false; //unsafe to place the queen here
}

//diagonal top left
int i = r; int j = c;
while(i >=0 && j >= 0) {
if(grid[i][j]) return false; //unsafe to place the queen here
i--; j--;
}

//diagonal top right
i = r; j = c;
while(i >=0 && j < n) {
if(grid[i][j]) return false; //unsafe to place the queen here
i--; j++;
}
return true;
}
}
49 changes: 49 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Time Complexity : O(m*n*3^L) where L is the length of the word
// Space Complexity : O(n)
// 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
//We perform DFS with backtracking we start with the cell whose character matches the first letter of word
//we recursively check for all its neighbors in right, top, left and down and also backtrack if no valid path found
class Solution {
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
int [][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};
for(int i = 0; i < m; i++) {
for(int j = 0; j <n; j++) {
if(board[i][j] == word.charAt(0)) {
if(backtrack(board, word, i, j, 0, dirs)) return true;
}
}
}
return false;

}

private boolean backtrack(char[][] board, String word, int i ,int j, int idx, int[][] dirs) {

//base
if (idx == word.length()) {
return true;
}
if(i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] == '#') return false;

//logic
if(board[i][j] == word.charAt(idx)) {
//only then you proceed
board[i][j] = '#'; //visited
//iterate over the neighbours
for(int[] dir: dirs) {
int nr = dir[0] + i;
int nc = dir[1] + j;
if(backtrack(board, word, nr, nc, idx + 1, dirs)) return true;
}
//backtrack
board[i][j] = word.charAt(idx);
}
return false;
}
}