diff --git a/Recursion/Trying_out_all_Combos_Hard/PalindromePartitioning.java b/Recursion/Trying_out_all_Combos_Hard/PalindromePartitioning.java new file mode 100644 index 0000000..0cf1321 --- /dev/null +++ b/Recursion/Trying_out_all_Combos_Hard/PalindromePartitioning.java @@ -0,0 +1,38 @@ +import java.util.*; +class Solution { + boolean isPalindrome(String s, int start, int end) { + while (start <= end) { + if (s.charAt(start++) != s.charAt(end--)) + return false; + } + return true; + } + public void fun(String s,List> ans,List ds,int ind){ + if(ind==s.length()){ + ans.add(new ArrayList<>(ds)); + return; + } + for(int i=ind;i> partition(String s) { + List> ans=new ArrayList<>(); + List ds=new ArrayList<>(); + fun(s,ans,ds,0); + return ans; + } +} +public class PalindromePartitioning { + public static void main(String[] args) { + Solution s=new Solution(); + List> ans=s.partition("aab"); + for(List i:ans){ + System.out.println(i); + } + } +} diff --git a/Recursion/Trying_out_all_Combos_Hard/RateInmaze.java b/Recursion/Trying_out_all_Combos_Hard/RateInmaze.java new file mode 100644 index 0000000..09e792d --- /dev/null +++ b/Recursion/Trying_out_all_Combos_Hard/RateInmaze.java @@ -0,0 +1,58 @@ +import java.util.ArrayList; +import java.util.List; + +class Choice { + char dName; + int dx, dy; + + Choice(char dName, int dx, int dy) { + this.dName = dName; + this.dx = dx; + this.dy = dy; + } +} + +class Solution { + public boolean isValid(int x, int y, int[][] mat) { + return x >= 0 && x < mat.length && y >= 0 && y < mat[0].length && mat[x][y] == 1; + } + + public void solve(int x, int y, StringBuilder path, List res, int[][] mat, List choices) { + if (x == mat.length - 1 && y == mat[0].length - 1) { + res.add(path.toString()); + return; + } + for (Choice choice : choices) { + int newX = x + choice.dx; + int newY = y + choice.dy; + + if (isValid(newX, newY, mat)) { + mat[x][y] = 0; // Mark as visited + path.append(choice.dName); + solve(newX, newY, path, res, mat, choices); + mat[x][y] = 1; // Unmark as visited + path.deleteCharAt(path.length() - 1); + } + } + } + + public List findPath(int[][] mat) { + StringBuilder path = new StringBuilder(); + List res = new ArrayList<>(); + List choices = new ArrayList<>(); + choices.add(new Choice('U', -1, 0)); + choices.add(new Choice('D', 1, 0)); + choices.add(new Choice('L', 0, -1)); + choices.add(new Choice('R', 0, 1)); + + if (mat[0][0] == 1) { + solve(0, 0, path, res, mat, choices); + } + + return res; + } +} + +public class RateInmaze { + +} diff --git a/Recursion/Trying_out_all_Combos_Hard/Sudoku_Solver.java b/Recursion/Trying_out_all_Combos_Hard/Sudoku_Solver.java index 2f423fd..1889053 100644 --- a/Recursion/Trying_out_all_Combos_Hard/Sudoku_Solver.java +++ b/Recursion/Trying_out_all_Combos_Hard/Sudoku_Solver.java @@ -1,4 +1,4 @@ -package Recursion.Trying_out_all_Combos_Hard; + public class Sudoku_Solver { public static boolean solve(char[][] board){