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

/**
* Using backtrack approach to start putting the queen in every possible column in each row to check if the grid is valid.
* If not valid, then we backtrack the position and try the next one.
*/
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> result = new ArrayList<>();
boolean[][] grid = new boolean[n][n];
backtrack(grid, n, 0, result);
return result;
}

private void backtrack(boolean[][] grid, int n, int r, List<List<String>> result) {
//base case
// we reach the last row
if (r == n) {
// converting boolean grid into result string
result.add(createResult(grid));
return;
}

for (int c = 0; c < n; c++) {
if (canPlace(grid, r, c, n)) {
// action
grid[r][c] = true;
//recurse
backtrack(grid, n, r + 1, result);
//backtrack
grid[r][c] = false;
}
}
}

private boolean canPlace(boolean[][] grid, int r, int c, int n) {
// checking if there is any queen in same column
for (int i = 0; i < r; i++) {
if (grid[i][c]) return false;
}

// checking if any queen in left diagonal
int i = r;
int j = c;
while (i >= 0 && j >= 0) {
if (grid[i][j])
return false;
i--;
j--;
}

// checking if any queen in right diagonal
i = r;
j = c;
while (i >= 0 && j < n) {
if (grid[i][j]) {
return false;
}

i--;
j++;
}

return true;
}

List<String> createResult(boolean[][] grid) {
List<String> li = new ArrayList<>();
for (int i = 0; i < grid.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j])
sb.append('Q');
else
sb.append('.');
}
li.add(sb.toString());
}

return li;
}


}
54 changes: 54 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Time Complexity : O(m*n * 4 ^ l) where l is the length of the word
// Space Complexity : O(l) where it will l recursion 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 use the backtracking approach to start finding the word from every possible index.
* Once a character matches, we convet it to #, look for next character.
* If the character was not found, backtrack the character back to its original character to continue the recursion.
*/
class Solution {
int[][] dirs = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

public boolean exist(char[][] board, String word) {
// checking for the start of the word from every possible index
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (isPresent(board, word, 0, i, j))
return true;
}
}

return false;
}

private boolean isPresent(char[][] board, String word, int idx, int r, int c) {
//base
// we reached the end of the word
if (idx == word.length()) {
return true;
}
// bounds check and check for the current path
if (r < 0 || r == board.length || c < 0 || c == board[0].length || board[r][c] == '#')
return false;

//logic
if (board[r][c] == word.charAt(idx)) {
// action
board[r][c] = '#';
// recurse
for (int[] dir : dirs) {
if (isPresent(board, word, idx + 1, r + dir[0], c + dir[1]))
return true;
}
//backtrack
board[r][c] = word.charAt(idx);
}

return false;
}
}