-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3839f5d
commit 371e727
Showing
3 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Recursion/Trying_out_all_Combos_Hard/PalindromePartitioning.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters