From c55e0db19169c69ffb3de3772307d321c1b907aa Mon Sep 17 00:00:00 2001 From: ashish-badhe Date: Sat, 8 Nov 2025 08:11:25 -0500 Subject: [PATCH 1/2] N-Queens & Word Search --- N-Queens.java | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ WordSearch.java | 71 ++++++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 N-Queens.java create mode 100644 WordSearch.java diff --git a/N-Queens.java b/N-Queens.java new file mode 100644 index 00000000..d187424c --- /dev/null +++ b/N-Queens.java @@ -0,0 +1,101 @@ +// 51. N-Queens +// https://leetcode.com/problems/n-queens/description/ + +// Solution => DFS + Backtracking + +/** + * Time Complexity: O(n * n!) since at every row we have options to place queen in factorial pattern i.e, n then n - 2, then n - 4 and so on + * Also, for checking safety we check above, left diagnl and right diagnl elements which is n + * + * Space Complexity: O(n) since in worst case we would have n calls till we reach last row to place queen + */ + +class Solution { + + List> result; + + public List> solveNQueens(int n) { + + result = new ArrayList<>(); + + boolean[][] board = new boolean[n][n]; + + // start dfs from first element + dfs(board, 0); + + return result; + } + + private void dfs(boolean[][] board, int r) { + + // base condition + if (r >= board.length) { // i.e we placed all queens + + List pattern = new ArrayList<>(); + + for (int i = 0; i < board.length; i++) { + StringBuilder sb = new StringBuilder(); + for (int j = 0; j < board[0].length; j++) { + if (board[i][j] == true) { + sb.append("Q"); + } else { + sb.append("."); + } + } + + pattern.add(sb.toString()); + } + + result.add(pattern); + return; + } + + // dfs for every col in a row + for (int c = 0; c < board[0].length; c++) { + if (isValid(board, r, c)) { + // action + board[r][c] = true; + // recurse + dfs(board, r + 1); + // backtrack + board[r][c] = false; + } + } + } + + private boolean isValid(boolean[][] board, int r, int c) { + // check above + int row = r; + int col = c; + while (row >= 0) { + if (board[row][col] == true) { + return false; + } + row--; + } + + // check diagonally right + row = r; + col = c; + while (row >= 0 && col < board[0].length) { + if (board[row][col] == true) { + return false; + } + row--; + col++; + } + + // check diagonally left + row = r; + col = c; + while (row >= 0 && col >= 0) { + if (board[row][col] == true) { + return false; + } + row--; + col--; + } + + return true; + } +} \ No newline at end of file diff --git a/WordSearch.java b/WordSearch.java new file mode 100644 index 00000000..c82e2d9c --- /dev/null +++ b/WordSearch.java @@ -0,0 +1,71 @@ +// 79. Word Search +// https://leetcode.com/problems/word-search/description/ + +// Solution => DFS + Backtracking + +/** + * Time Complexity: O(m * n * 3 ^ l) since we iterate over enitre matrix we have m * n + * and at each dfs we take 3 decisions and we go till we find the word, i.e depth is l + * + * Space Complexity: O(l) where l is length of the word. Since in worst case we would have l call in call stack + */ + +class Solution { + int m; + int n; + int[][] dirs; + boolean found; + + public boolean exist(char[][] board, String word) { + m = board.length; + n = board[0].length; + + found = false; + + dirs = new int[][] { {1,0}, {-1,0}, {0,1}, {0,-1}}; + + // start new dfs for every matching first char + for(int i = 0 ; i < m ; i++){ + for(int j = 0 ; j < n ; j++){ + char ch = word.charAt(0); + + if(board[i][j] == ch){ + // mark it visited + board[i][j] = '#'; + dfs(board, i, j, 1, word); + // backtrack + board[i][j] = ch; + } + } + } + + return found; + } + + private void dfs(char[][] board, int i, int j, int idx, String word){ + // base condition + if(idx >= word.length()){ + found = true; + return; + } + + // logic + + // dfs on all 4 directions + for(int[] dir: dirs){ + int r = i + dir[0]; + int c = j + dir[1]; + + char ch = word.charAt(idx); + + if(r >= 0 && c >= 0 && r < m && c < n && ch == board[r][c]){ + // action + board[r][c] = '#'; + // recurse + dfs(board, r, c, idx + 1, word); + // backtrack + board[r][c] = ch; + } + } + } +} \ No newline at end of file From 57477974d1c83d7b620a75f18af7a7b5a86bb55c Mon Sep 17 00:00:00 2001 From: ashish-badhe Date: Sat, 8 Nov 2025 09:57:58 -0500 Subject: [PATCH 2/2] fix for SC and skip dfs if found --- N-Queens.java | 2 +- WordSearch.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/N-Queens.java b/N-Queens.java index d187424c..e8bbcb5f 100644 --- a/N-Queens.java +++ b/N-Queens.java @@ -7,7 +7,7 @@ * Time Complexity: O(n * n!) since at every row we have options to place queen in factorial pattern i.e, n then n - 2, then n - 4 and so on * Also, for checking safety we check above, left diagnl and right diagnl elements which is n * - * Space Complexity: O(n) since in worst case we would have n calls till we reach last row to place queen + * Space Complexity: O(n * n) since in worst case we would have n calls till we reach last row to place queen and n for board space */ class Solution { diff --git a/WordSearch.java b/WordSearch.java index c82e2d9c..52a84cc7 100644 --- a/WordSearch.java +++ b/WordSearch.java @@ -29,7 +29,7 @@ public boolean exist(char[][] board, String word) { for(int j = 0 ; j < n ; j++){ char ch = word.charAt(0); - if(board[i][j] == ch){ + if(board[i][j] == ch & !found){ // mark it visited board[i][j] = '#'; dfs(board, i, j, 1, word);