Problem Statement:
Given a string s
, the task is to partition the string into all possible palindrome substrings. We need to find and return all possible valid partitions where each substring is a palindrome.
The ispalindrome
function checks whether a given substring of string s
is a palindrome.
s
: The input string.start
: The starting index of the substring.end
: The ending index of the substring.false
, indicating the substring is not a palindrome.true
and prints the palindrome substring.The calculate
function recursively generates all valid partitions of the input string s
.
index
: The current index in the string.t
: A vector of vectors that stores the valid palindrome partitions.s
: The input string.path
: A vector that stores the current partition path.index
.index
to i
is a palindrome, we add it to the path
.index
and the current path
.index
reaches the end of the string, we’ve found a valid partition, and we add it to the result (t
).path
.This is the main function that initializes the process of palindrome partitioning.
s
: The input string.t
) to store the valid palindrome partitions.path
) to store the current partition path.calculate
function is called with initial parameters to start the recursive partition generation process.t
containing all valid palindrome partitions.Input:
s = "aab"
Output:
[["a", "a", "b"], ["aa", "b"]]
Explanation:
The string "aab"
can be partitioned in two valid ways:
"a"
, "a"
, "b"
"aa"
, "b"
Input:
s = "a"
Output:
[["a"]]
Explanation:
The string "a"
is already a palindrome, so the only partition is itself.
Input:
s = "racecar"
Output:
[["r", "a", "c", "e", "c", "a", "r"], ["racecar"]]
Explanation:
The string "racecar"
can be partitioned in two valid ways:
"r"
, "a"
, "c"
, "e"
, "c"
, "a"
, "r"
"racecar"
(the whole string is a palindrome)Input:
s = "abc"
Output:
[["a", "b", "c"]]
Explanation:
Since no two characters form a valid palindrome substring except for the single characters, the only valid partition is each character individually.
Input:
s = "aabb"
Output:
[["a", "a", "b", "b"], ["aa", "b", "b"], ["a", "a", "bb"]]
Explanation:
The string "aabb"
can be partitioned into the following valid palindrome partitions:
"a"
, "a"
, "b"
, "b"
"aa"
, "b"
, "b"
"a"
, "a"
, "bb"
https://leetcode.com/problems/palindrome-partitioning/description/
Loading component...
Loading component...
INPUT: s = "aab";
OUTPUT: [ [ "a", "a" , "b" ],[ "aa","b"] ]
public static void main(String args[]) {
String s = "aabb";
List < List < String >> ans = partition(s);
int n = ans.size();
System.out.println("The Palindromic partitions are :-");
System.out.print(" [ ");
for (int i = 0; i < ans.size(); i++) {
System.out.print("[ ");
for (int j = 0; j < ans.get(i).size(); j++) {
System.out.print(ans.get(i).get(j) + " ");
}//Loop End
System.out.print("] ");
}//Loop End
System.out.print("]");
}//function end
public static List < List < String >> partition(String s) {
List < List < String >> res = new ArrayList < > ();
List < String > path = new ArrayList < > ();
calculate(0, s, path, res);
return res;
}//function end
static void calculate(int index, String s, List < String > path, List < List < String >> res) {
if (index == s.length()) {
res.add(new ArrayList < > (path));
return;
}//If End
for (int i = index; i < s.length(); ++i) {
if (isPalindrome(s, index, i)) {
path.add(s.substring(index, i + 1));
calculate(i + 1, s, path, res);
path.remove(path.size() - 1);
}//If End
}//Loop End
}//function end
static boolean isPalindrome(String s, int start, int end) {
while (start <= end) {
if (s.charAt(start++) != s.charAt(end--)){
return false;
}//If End
}//Loop End
return true;
}//function end