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
87 changes: 87 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//TC - O(n! * n)
//SC - O(n^2) + O(n)

/*
We place one queen in each row and try all possible columns using backtracking, moving to the next row only when a safe position is found.
Before placing a queen, we check the column and both upper diagonals to ensure it does not conflict with previously placed queens.
The board is updated in place and restored during backtracking, while recursion proceeds row by row until a valid arrangement is formed.
*/


import java.util.ArrayList;
import java.util.List;

public class Problem1 {
List<List<String>> res;
public List<List<String>> solveNQueens(int n) {
res = new ArrayList<>();
boolean[][] board = new boolean[n][n];
helper(board, 0);
return res;
}

private void helper(boolean[][] board, int row) {
//base
if(row == board.length) {
List<String> li = new ArrayList<>();
for(int i = 0;i<board.length;i++) {
StringBuilder sb = new StringBuilder();
for(int j = 0;j<board[i].length;j++) {
if(board[i][j]) {
sb.append("Q");
} else {
sb.append(".");
}
}
li.add(sb.toString());
}
res.add(li);
return;
}

//logic
for(int c = 0;c<board[row].length;c++) {
if(isSafe(board, row, c)) {
//action
board[row][c] = true;
//recurse
helper(board, row+1);
//backtrack
board[row][c] = false;
}
}
}

private boolean isSafe(boolean[][] board, int row, int col) {
//up
for(int i = 0;i<=row;i++) {
if(board[i][col]) {
return false;
}
}

//diagonal up right
int i = row;
int j = col;
while(i >= 0 && j < board[i].length) {
if(board[i][j]) {
return false;
}
i--;
j++;
}

//diagonal up left
i = row;
j = col;
while(i >= 0 && j >=0) {
if(board[i][j]) {
return false;
}
i--;
j--;
}

return true;
}
}
56 changes: 56 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//TC - O(m * n * 3 ^ L)
//SC - O(L) ~ recursion stck space

/*
We scan the entire board and start a DFS from any cell that matches the first character of the word.
From each starting cell, the DFS tries all possible paths by moving in four directions while matching characters and backtracking when a path fails.
This exploration continues until the full word is matched or all possible paths from that cell are exhausted.
*/

public class Problem2 {
int[][] dirs = {{0,1}, {0,-1}, {1,0}, {-1,0}};
public boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;

for(int i = 0;i<m;i++) {
for(int j = 0;j<n;j++) {
if(board[i][j] == word.charAt(0)) {
if(isExists(board, word, i, j, 0)) {
return true;
}
}
}
}

return false;
}

private boolean isExists(char[][] board, String word, int row, int col, int idx) {

if(idx == word.length()) {
return true;
}

if(row < 0 || col < 0 || row >= board.length || col >= board[row].length || board[row][col] != word.charAt(idx)) {
return false;
}


char ch = board[row][col];
board[row][col] = '$';

for(int[] dir : dirs) {
int nr = row + dir[0];
int nc = col + dir[1];

if(isExists(board, word, nr, nc, idx+1)) {
return true;
}
}

board[row][col] = ch;
return false;

}
}