From 6124ac7a5a3553720f0b08e5726915548d650687 Mon Sep 17 00:00:00 2001 From: sainathek Date: Thu, 26 Mar 2026 22:23:36 -0400 Subject: [PATCH] Done Backtracking-3 --- problem1.java | 81 ++++++++++++++++++++++++++++++++++++++ problem2.java | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..531e0edb --- /dev/null +++ b/problem1.java @@ -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; + } + } \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..f294e26d --- /dev/null +++ b/problem2.java @@ -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> result; + + public List> 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 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; + } + } \ No newline at end of file