From 0baa366f2729df35ec4347f6cf2d4d198e479df2 Mon Sep 17 00:00:00 2001 From: Vidya Sreekumar Date: Fri, 26 Sep 2025 12:55:47 -0500 Subject: [PATCH] Completed Backtracking-3 --- Problem-1 | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem-2 | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 Problem-1 create mode 100644 Problem-2 diff --git a/Problem-1 b/Problem-1 new file mode 100644 index 00000000..dd4c77bc --- /dev/null +++ b/Problem-1 @@ -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> result; + public List> 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 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; + } +} \ No newline at end of file diff --git a/Problem-2 b/Problem-2 new file mode 100644 index 00000000..4f36c558 --- /dev/null +++ b/Problem-2 @@ -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; + } +} \ No newline at end of file