Skip to content

Commit

Permalink
add recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
BamaCharanChhandogi committed Aug 14, 2024
1 parent 3839f5d commit 371e727
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
38 changes: 38 additions & 0 deletions Recursion/Trying_out_all_Combos_Hard/PalindromePartitioning.java
Original file line number Diff line number Diff line change
@@ -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<List<String>> ans,List<String> ds,int ind){
if(ind==s.length()){
ans.add(new ArrayList<>(ds));
return;
}
for(int i=ind;i<s.length();i++){
if(isPalindrome(s,ind,i)){
ds.add(s.substring(ind,i+1));
fun(s,ans,ds,i+1);
ds.remove(ds.size()-1);
}
}
}
public List<List<String>> partition(String s) {
List<List<String>> ans=new ArrayList<>();
List<String> ds=new ArrayList<>();
fun(s,ans,ds,0);
return ans;
}
}
public class PalindromePartitioning {
public static void main(String[] args) {
Solution s=new Solution();
List<List<String>> ans=s.partition("aab");
for(List<String> i:ans){
System.out.println(i);
}
}
}
58 changes: 58 additions & 0 deletions Recursion/Trying_out_all_Combos_Hard/RateInmaze.java
Original file line number Diff line number Diff line change
@@ -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<String> res, int[][] mat, List<Choice> 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<String> findPath(int[][] mat) {
StringBuilder path = new StringBuilder();
List<String> res = new ArrayList<>();
List<Choice> 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 {

}
2 changes: 1 addition & 1 deletion Recursion/Trying_out_all_Combos_Hard/Sudoku_Solver.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Recursion.Trying_out_all_Combos_Hard;


public class Sudoku_Solver {
public static boolean solve(char[][] board){
Expand Down

0 comments on commit 371e727

Please sign in to comment.