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
81 changes: 81 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
Time Complexity : O(M * N * 4^L)
Explanation:
- We start DFS from each cell → M * N
- For each step, we explore up to 4 directions
- Depth of recursion = length of word (L)
So worst case: O(M * N * 4^L)

Space Complexity : O(L)
Explanation:
Recursion depth can go up to length of the word.

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially forgot to mark visited cells, which caused revisiting
the same cell and incorrect paths.
Fixed it by marking the cell as '#' during recursion and restoring
it after backtracking.
Also needed to carefully handle boundary conditions and base case
when the entire word is matched.
*/

class Solution {


public boolean exist(char[][] board, String word) {

int m = board.length;
int n = board[0].length;

int[][] adj = {{1,0}, {-1,0}, {0,1}, {0,-1}};

// Try starting from every cell
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {

if (board[i][j] == word.charAt(0)) {
if (helper(board, i, j, adj, word, 0, m, n)) {
return true;
}
}
}
}

return false;
}

private boolean helper(char[][] board, int r, int c, int[][] adj,
String word, int idx, int m, int n) {

// Base case: matched entire word
if (idx == word.length()) {
return true;
}

// Boundary + mismatch check
if (r < 0 || c < 0 || r >= m || c >= n || board[r][c] != word.charAt(idx)) {
return false;
}

// Mark visited
char temp = board[r][c];
board[r][c] = '#';

// Explore all 4 directions
for (int[] dir : adj) {
int nr = r + dir[0];
int nc = c + dir[1];

if (helper(board, nr, nc, adj, word, idx + 1, m, n)) {
return true;
}
}

// Backtrack
board[r][c] = temp;

return false;
}
}
107 changes: 107 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
Time Complexity : O(N!)
Explanation:
We try placing a queen in each column for every row.
In worst case, number of configurations grows factorially.
For each placement, we check safety in O(N).

Space Complexity : O(N^2)
Explanation:
Board takes O(N^2) space and recursion stack goes up to N.

Did this code successfully run on LeetCode : Yes

Any problem you faced while coding this :
Initially struggled with validating safe positions for queens.
Fixed it by checking:
1) Same column (upward)
2) Left diagonal
3) Right diagonal
Also needed to carefully backtrack (place → recurse → remove)
to explore all valid configurations.
*/
class Solution {



List<List<String>> result;

public List<List<String>> solveNQueens(int n) {

this.result = new ArrayList<>();
boolean[][] board = new boolean[n][n];

helper(n, 0, board);
return result;
}

private void helper(int n, int r, boolean[][] board) {

// Base case: all queens placed
if (r == n) {

List<String> li = new ArrayList<>();

for (int i = 0; i < n; i++) {
StringBuilder row = new StringBuilder();

for (int j = 0; j < n; j++) {
if (board[i][j]) row.append('Q');
else row.append('.');
}

li.add(row.toString());
}

result.add(li);
return;
}

// Try placing queen in each column
for (int c = 0; c < n; c++) {

if (isSafeToPlace(n, r, c, board)) {

board[r][c] = true;

helper(n, r + 1, board);

// Backtrack
board[r][c] = false;
}
}
}

private boolean isSafeToPlace(int n, int i, int j, boolean[][] board) {

int r, c;

// Check column (up)
r = i;
c = j;
while (r >= 0) {
if (board[r][c]) return false;
r--;
}

// Check left diagonal
r = i;
c = j;
while (r >= 0 && c >= 0) {
if (board[r][c]) return false;
r--;
c--;
}

// Check right diagonal
r = i;
c = j;
while (r >= 0 && c < n) {
if (board[r][c]) return false;
r--;
c++;
}

return true;
}
}