Skip to content

Back tracking #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.thealgorithm.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
* @author: Subham Santra
* @implNote
* <p>This is a pure back-tracking approach This problem can be solved using dynamic programming
* also, I will add DP code in DP section</p>
*/
public class PalindromePartitioning {

Boolean[][] memo;

public List<List<String>> partition(String s) {
memo = new Boolean[s.length()][s.length()];
List<List<String>> possiblePalindromes = new ArrayList<>();
partition(s, 0, new ArrayList<>(), possiblePalindromes);
return possiblePalindromes;
}

void partition(String s, int lo, List<String> current, List<List<String>> possiblePalindromes) {
if (lo == s.length()) {
possiblePalindromes.addFirst(new ArrayList<>(current));
return;
}

for (int p = lo; p < s.length(); ++p) {
if (isPalindrome(s, lo, p)) {
current.addLast(s.substring(lo, p + 1));
partition(s, p + 1, current, possiblePalindromes);
current.removeLast();
}
}
}

boolean isPalindrome(String s, int l, int h) {
if (memo[l][h] == null) {
for (int i = l, j = h; i <= j; ++i, --j) {
if (s.charAt(i) != s.charAt(j)) {
return memo[l][h] = false;
}
}
return memo[l][h] = true;
}
return memo[l][h];
}

public static void main(String[] args) {
System.out.println(new PalindromePartitioning().partition("baab"));
System.out.println(new PalindromePartitioning().partition("A"));
System.out.println(new PalindromePartitioning().partition("aab"));
System.out.println(new PalindromePartitioning().partition(""));
System.out.println(new PalindromePartitioning().partition("aababbababbabbabba"));
}
}