Tutorialspoint
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
Backtracking PwCZomato
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

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

Steps to solve by this approach:

 Step 1: Precompute whether each substring is a palindrome using dynamic programming.
 Step 2: Set up a backtracking algorithm to explore all possible partitions.
 Step 3: For each position in the string, check if the substring from the start to the current position is a palindrome.
 Step 4: If it is a palindrome, add it to the current partition and recursively solve for the remaining string.
 Step 5: When we reach the end of the string, add the current partition to the result.
 Step 6: Use backtracking to explore all possible combinations of partitions.
 Step 7: Return all valid palindrome partitions.

Submitted Code :