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
74 changes: 74 additions & 0 deletions Nqueens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Time Complexity : O(n!)
// Space Complexity : O(n*n) for board + 0(n) recursive stack
// Did this code successfully run on Leetcode : Yes
// Approach : We start from 1st row 1st col by inserting queen and explore the next row's all columns if we can insert a queen by checking
// the column, upper left diagonal and upper right diagonal for an existing queen. We repeat until we complete the board, construct
// the result and return.

class Solution {
List<List<String>> result;

public List<List<String>> solveNQueens(int n) {
this.result = new ArrayList<>();
boolean[][] arr = new boolean[n][n];//create a chess board 2d array
helper(arr, 0, n);
return result;
}

public void helper(boolean[][] arr, int row, int n) {
//base case
if (row == arr.length) { //when all row are parsed
List<String> li = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < n; j++) {
if (arr[i][j] == true) {
sb.append("Q");
} else {
sb.append(".");
}

}
li.add(sb.toString());
}
result.add(li);

}

for (int c = 0; c < n; c++) {
if (isSafe(arr, row, c)) { //checking if safe to add
arr[row][c] = true;
helper(arr, row + 1, n);
arr[row][c] = false; //backtrack
}
}

}

private boolean isSafe(boolean[][] arr, int row, int col) {
for (int i = row - 1; i >= 0; i--) { //column check
if (arr[i][col]) {
return false;
}
}
int i = row;
int j = col;
while (i >= 0 && j >= 0) { //upper left diagonal check
if (arr[i][j]) {
return false;
}
i--;
j--;
}
int a = row;
int b = col;
while (a >= 0 && b < arr.length) { //upper right diagonal check
if (arr[a][b]) {
return false;
}
a--;
b++;
}
return true;
}
}
52 changes: 52 additions & 0 deletions wordSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity : O(m*n*3^(L)) where L is the length of word
// Space Complexity : O(L)
// Did this code successfully run on Leetcode : Yes
// Approach : Performed DFW with backtracking to get to the solution. We start performing DFS when we encounter first char in word while
// parsing the matrix. We mark the visited chars as # and continue exploring neighbors. When the next char si not found we backtrack and
// unmark the visited node so that it can be visited again. Continuw until we reach the end of the word length.

class Solution {
int[][] directions;
int m;
int n;

public boolean exist(char[][] board, String word) {
this.directions = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };// directions array
this.m = board.length;
this.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)) {//when first char is word is found
if (helper(board, word, i, j, 0)) {
return true;
}

}
}
}
return false;
}

private boolean helper(char[][] board, String word, int i, int j, int index) {
if (index == word.length()) { //base case
return true;
}
if (i < 0 || j < 0 || i == m || j == n || board[i][j] == '#') { //bounds and visited condition check
return false;
}

if (board[i][j] == word.charAt(index)) { //match found
board[i][j] = '#';// marks as visited
for (int[] dir : directions) {
int nr = i + dir[0]; // neighbour row
int nc = j + dir[1]; // neighbour column
if (helper(board, word, nr, nc, index + 1)) {
return true;
}
}
board[i][j] = word.charAt(index); //backtrack
}
return false;

}
}