-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
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,73 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
//Palindromic Decomposition Of A String | ||
// | ||
// Find all palindromic decompositions of a given string s. | ||
// | ||
// A palindromic decomposition of string is a decomposition of the string into substrings, such that all those substrings are valid palindromes. | ||
// | ||
// Example | ||
// Input: "abracadabra" | ||
// | ||
// Output: [ "a|b|r|a|c|a|d|a|b|r|a", "a|b|r|a|c|ada|b|r|a", "a|b|r|aca|d|a|b|r|a" ] | ||
|
||
|
||
public class GeneratePalindromicDecomposition { | ||
static String[] generate_palindromic_decompositions(String s) { | ||
List<String> result = new ArrayList<>(); | ||
String[][] palindromeMap = new String[s.length()][s.length()]; | ||
generate_palindromic_decompositions_recursive(s.toCharArray(), result, new char[s.length() * 2], 0, 0, palindromeMap); | ||
return result.toArray(new String[result.size()]); | ||
} | ||
|
||
static void generate_palindromic_decompositions_recursive(char[] s, List<String> result, char[] slate, int index, int slateIx, String[][] palindromeMap) { | ||
if (index >= s.length) { | ||
result.add(new String(slate, 0, slateIx - 1)); | ||
return; | ||
} | ||
for (int i = index; i < s.length; i++) { | ||
slate[slateIx++] = s[i]; | ||
if (isPalindrome(s, index, i, palindromeMap) == "Y") { | ||
slate[slateIx] = '|'; | ||
generate_palindromic_decompositions_recursive(s, result, slate, i + 1, slateIx + 1, palindromeMap); | ||
} | ||
} | ||
} | ||
|
||
static String isPalindrome(char[] s, int left, int right, String[][] palindromeMap) { | ||
if (palindromeMap[left][right] != null) { | ||
return palindromeMap[left][right]; | ||
} | ||
String result = "Y"; | ||
while (left < right) { | ||
if (s[left] != s[right]) { | ||
result = "N"; | ||
break; | ||
} | ||
left++; | ||
right--; | ||
} | ||
palindromeMap[left][right] = result; | ||
return result; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
|
||
} |
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,64 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
//https://www.youtube.com/watch?v=4ykBXGbonlA | ||
public class PrintPalindromicPartition { | ||
|
||
// Palindromic Decomposition Of A String | ||
// | ||
// Find all palindromic decompositions of a given string s. | ||
// | ||
// A palindromic decomposition of string is a decomposition of the string into substrings, such that all those substrings are valid palindromes. | ||
// | ||
// Example | ||
// Input: "abracadabra" | ||
// | ||
// Output: [ "a|b|r|a|c|a|d|a|b|r|a", "a|b|r|a|c|ada|b|r|a", "a|b|r|aca|d|a|b|r|a" ] | ||
|
||
|
||
|
||
|
||
public static String[] generate_palindromic_decompositions(String s) { | ||
List<String> slate = new ArrayList<>(); | ||
List<String> result = new ArrayList<>(); | ||
helper(s, 0, slate, result); | ||
String[] array = result.toArray(new String[0]); | ||
return array; | ||
} | ||
|
||
private static void helper(String s, int idx, List<String> slate, List<String> result) { | ||
|
||
if(idx == s.length()) { | ||
String p = String.join("|" , slate); | ||
result.add(p); | ||
return; | ||
} | ||
|
||
for(int i = idx; i < s.length();i++) { | ||
if (isPalindrom(s, idx, i)) { | ||
slate.add(s.substring(idx, i + 1)); | ||
helper(s, i+1, slate, result); | ||
slate.remove(slate.size() - 1); | ||
} | ||
} | ||
} | ||
|
||
private static boolean isPalindrom(String s, int from, int to) { | ||
if(from == to) return true; | ||
while(from < to) { | ||
if(s.charAt(from) != s.charAt(to)) return false; | ||
from++; | ||
to--; | ||
} | ||
return true; | ||
} | ||
|
||
public static void main(String[] args) { | ||
String[] a = generate_palindromic_decompositions("abracadabra"); | ||
for(int i = 0; i < a.length; i++) { | ||
System.out.println(a[i]); | ||
} | ||
} | ||
|
||
} |