From 4ffdf7e263b142c919f60ab5fc1c526471e0d779 Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sun, 1 Feb 2026 00:45:04 -0600 Subject: [PATCH 1/2] Add word search algorithm in Leetcode_79 Implement a solution to check if a word exists in a 2D board. --- Leetcode_79.java | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Leetcode_79.java diff --git a/Leetcode_79.java b/Leetcode_79.java new file mode 100644 index 00000000..e4ba2a4b --- /dev/null +++ b/Leetcode_79.java @@ -0,0 +1,54 @@ +//Have a dir array +//Call helper from every index +//In helper - check all neighbour blocks with index array +//If mataches with word characters - return true +//TC: O(m * n * 4^L) where L = word.length() +//SC: O(d) + + +class Solution { + int[][] dir; + boolean flag; + public boolean exist(char[][] board, String word) { + this.dir=new int[][]{{1,0},{-1,0},{0,1},{0,-1}}; + this.flag=false; + for(int i=0;i=board.length || c>=board[0].length){ + return; + } + if(board[r][c]!=word.charAt(index)) return; + + if(index==word.length()-1){ + flag=true; + return; + } + + + board[r][c]='#'; + + //actual logic + for(int[] d:dir){ + int nr=r+d[0]; + int nc=c+d[1]; + + helper(board,word,index+1,nr,nc); + + } + board[r][c]=word.charAt(index); + } +} From 3c7c7dc92164b77033ecad1ce8d04c67041df60c Mon Sep 17 00:00:00 2001 From: Sreeja-99 <75175169+Sreeja-99@users.noreply.github.com> Date: Sun, 1 Feb 2026 00:45:38 -0600 Subject: [PATCH 2/2] Add N-Queens backtracking solution in Leetcode_51.java Implement N-Queens solution using backtracking. --- Leetcode_51.java | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Leetcode_51.java diff --git a/Leetcode_51.java b/Leetcode_51.java new file mode 100644 index 00000000..606ae867 --- /dev/null +++ b/Leetcode_51.java @@ -0,0 +1,88 @@ +//Backtracking +//Declare a grid - n*n +//Check the possibilty by giving first row first element +//Helper recurssion start with first row first element +//It will check whether it is a valid or not +//If valid, mark as true and go to second row +//Check for the valid block. If valid go to third row and so on +//At any place where the block is not valid, backtrack. Means make that block of grid as false +//As a base case, add the path to result +//TC: n! +//SC: O( n^2 ) for the board + O( n ) recursive stack space +class Solution { + public List> solveNQueens(int n) { + List> ans=new ArrayList<>(); + boolean[][] grid=new boolean[n][n]; + helper(grid,0,ans); + return ans; + + + } + + private void helper(boolean[][] grid,int i,List> result){ + //base case + if(i==grid.length){ + List curr=new ArrayList<>(); + for(int r=0;r=0 && r=0 && c>=0){ + if(grid[r][c]==true){ + return false; + } + r-=1; + c-=1; + } + + r=i-1; + c=j+1; + while(r>=0 && c