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
77 changes: 77 additions & 0 deletions Problem-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Time Complexity: O(n * n!)
// Space Complexity: O(n^2)

// Place queens row by row recursively by checking if its safe to place
// To check the safety of a position check the column up, left diagonal up and right diagonal up to see if a queen has been placed in those lines using a boolean matrix
// Create the result list from the boolean matrix after placing all the queens

class Solution {
List<List<String>> result;
public List<List<String>> solveNQueens(int n) {
this.result = new ArrayList<>();
boolean[][] grid = new boolean[n][n];
helper(grid, 0, n);
return result;
}

private void helper(boolean[][] grid, int row, int n) {
if(row == n) {
// Create result list from the boolean grid which contains the positions of the queens
List<String> list = 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(".");
}
}
list.add(sb.toString());
}
result.add(list);
return;
}

for(int j = 0; j < n; j++) {
// Check if a queen can placed at this position
if(isSafe(grid, row, j, n)) {
grid[row][j] = true;
helper(grid, row+1, n);
grid[row][j] = false;
}
}
}

private boolean isSafe(boolean[][] grid, int r, int c, int n) {
// check column up
int i = r;
int j = c;
while(i >= 0) {
if(grid[i][j])
return false;
i--;
}

// check left diagonal up
i = r;
while(i >= 0 && j >= 0) {
if(grid[i][j])
return false;
i--;
j--;
}

// check right diagonal up
i = r;
j = c;
while(i >= 0 && j < n) {
if(grid[i][j])
return false;
i--;
j++;
}
return true;
}
}
45 changes: 45 additions & 0 deletions Problem-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity: O(m*n*4^l) , l->length of word
// Space Complexity: O(l)

// Traverse through the board, and do dfs from all the starting characters of the word on the board
// mark a position on the board as visited if it matches with the current character in the word
// if it matches all characters in the word return true, otherwise backtrack

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

for(int i = 0 ; i < m; i++) {
for(int j = 0; j < n; j++) {
// If the first character matches do dfs on the remaining characters
if(board[i][j] == word.charAt(0)) {
if(backtrack(board, word, i, j, 0))
return true;
}
}
}
return false;
}

private boolean backtrack(char[][] board, String word, int i, int j, int idx) {
if(idx == word.length())
return true;
if(i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] == '#' || board[i][j] != word.charAt(idx))
return false;

board[i][j] = '#'; // marking as visited
// check neighbours
for(int[] d : dir) {
int r = d[0] + i;
int c = d[1] + j;
if(backtrack(board, word, r, c, idx+1))
return true;
}
board[i][j] = word.charAt(idx); //backtrack

return false;
}
}