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...