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
67 changes: 67 additions & 0 deletions N-queens
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// time O( n! )

// Space Complexity: O( n^2 ) for the board + O( n ) recursive stack space




class Solution {
List<List<String>> result;
boolean[][] board;
boolean[] colSet;
boolean[] mainDiagSet;
boolean[] antiDiagSet;
public List<List<String>> solveNQueens(int n) {
this.result = new ArrayList<>();
this.board = new boolean[n][n];
this.colSet = new boolean[2*n];
this.mainDiagSet = new boolean[2*n];
this.antiDiagSet = new boolean[2*n];

helper(0, n);

return result;
}

private void helper(int r , int n){

if(r == n){
List<String> list = new ArrayList<>();
for(int i=0; i<n; i++){
StringBuilder sb = new StringBuilder();
for(int j=0; j<n; j++){
if(board[i][j]){
sb.append("Q");
}else{
sb.append(".");
}
}
list.add(sb.toString());
}
result.add(list);
return;
}

for(int c=0; c<n; c++){

int md = r+c;
int ad = r-c + n;

if(!colSet[c]&& !mainDiagSet[md] && !antiDiagSet[ad])
{
colSet[c] = true;
mainDiagSet[md] = true;
antiDiagSet[ad] = true;

board[r][c] = true;
helper(r+1, n);
board[r][c] = false;

colSet[c] = false;
mainDiagSet[md] = false;
antiDiagSet[ad] = false;

}
}
}
}
83 changes: 83 additions & 0 deletions Word search
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Time Complexity: O(m * n * 4^L) where L = word.length()

// Space Complexity: O(M * N * L)




import java.util.*;

class Solution {
public boolean exist(char[][] board, String word) {
if (board == null || board.length == 0 || board[0].length == 0) return false;

int rows = board.length;
int cols = board[0].length;
int[][] directions = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == word.charAt(0)) {
if (bfs(board, word, i, j, directions)) return true;
}
}
}

return false;
}

private boolean bfs(char[][] board, String word, int startI, int startJ, int[][] directions) {
int rows = board.length;
int cols = board[0].length;

Queue<State> queue = new LinkedList<>();
boolean[][] visited = new boolean[rows][cols];
visited[startI][startJ] = true;
queue.offer(new State(startI, startJ, 0, visited));

while (!queue.isEmpty()) {
State curr = queue.poll();
int i = curr.i, j = curr.j, idx = curr.idx;
boolean[][] vis = curr.visited;

if (idx == word.length() - 1) return true;

for (int[] dir : directions) {
int newI = i + dir[0];
int newJ = j + dir[1];

if (newI >= 0 && newJ >= 0 && newI < rows && newJ < cols && !vis[newI][newJ]) {
if (board[newI][newJ] == word.charAt(idx + 1)) {
boolean[][] newVis = deepCopy(vis);
newVis[newI][newJ] = true;
queue.offer(new State(newI, newJ, idx + 1, newVis));
}
}
}
}

return false;
}

private boolean[][] deepCopy(boolean[][] original) {
int m = original.length;
int n = original[0].length;
boolean[][] copy = new boolean[m][n];
for (int i = 0; i < m; i++) {
System.arraycopy(original[i], 0, copy[i], 0, n);
}
return copy;
}

private static class State {
int i, j, idx;
boolean[][] visited;

State(int i, int j, int idx, boolean[][] visited) {
this.i = i;
this.j = j;
this.idx = idx;
this.visited = visited;
}
}
}