From f0127097b94b83b239d98bcd594e2c06f4f65c76 Mon Sep 17 00:00:00 2001 From: kalyan Date: Sat, 8 Nov 2025 15:56:08 -0600 Subject: [PATCH] Backtracking-3 solutions --- Nqueens.java | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ wordSearch.java | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 Nqueens.java create mode 100644 wordSearch.java diff --git a/Nqueens.java b/Nqueens.java new file mode 100644 index 00000000..e847722d --- /dev/null +++ b/Nqueens.java @@ -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> result; + + public List> 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 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; + } +} \ No newline at end of file diff --git a/wordSearch.java b/wordSearch.java new file mode 100644 index 00000000..c88b541e --- /dev/null +++ b/wordSearch.java @@ -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; + + } +} \ No newline at end of file