
Problem
Solution
Submissions
Palindrome Partitioning
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C# program to implement the Partition(string s)
function, which returns all possible palindrome partitioning of the input string.
Example 1
- Input: s = "aab"
- Output: [["a","a","b"],["aa","b"]]
- Explanation:
- Step 1: Generate all possible ways to partition the string.
- Step 2: Check if each partition consists only of palindromes.
- Partition ["a","a","b"]: "a" is a palindrome, "a" is a palindrome, "b" is a palindrome.
- Partition ["aa","b"]: "aa" is a palindrome, "b" is a palindrome.
- Partition ["a","ab"]: "a" is a palindrome, but "ab" is not a palindrome (rejected).
- Partition ["aab"]: "aab" is not a palindrome (rejected).
- Step 3: Collect all valid partitions where each substring is a palindrome.
- Step 4: Return the list of valid palindrome partitions [["a","a","b"],["aa","b"]].
Example 2
- Input: s = "abba"
- Output: [["a","b","b","a"],["a","bb","a"],["abba"]]
- Explanation:
- Step 1: Generate all possible ways to partition the string.
- Step 2: Check if each partition consists only of palindromes.
- Partition ["a","b","b","a"]: All individual characters are palindromes.
- Partition ["a","bb","a"]: "a" is a palindrome, "bb" is a palindrome, "a" is a palindrome.
- Partition ["abba"]: "abba" is a palindrome.
- Other partitions like ["ab","ba"] are rejected because "ab" is not a palindrome.
- Step 3: Collect all valid partitions where each substring is a palindrome.
- Step 4: Return the list of valid palindrome partitions [["a","b","b","a"],["a","bb","a"],["abba"]].
Constraints
- 1 ≤ s.length ≤ 16
- s consists of only lowercase English letters
- Time Complexity: O(N * 2^N) where N is the length of the input string
- Space Complexity: O(N * 2^N) for storing all possible palindrome partitions
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use backtracking to explore all possible partitions
- Precompute palindrome checks using dynamic programming for efficiency
- For each position, decide whether to create a partition
- Recursively solve the problem for the remaining substring