Tutorialspoint
Problem
Solution
Submissions

Palindrome Partitioning II

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C program to find the minimum number of cuts needed to partition a string such that every substring of the partition is a palindrome. Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s.

Example 1
  • Input: s = "aab"
  • Output: 1
  • Explanation:
    • The string "aab" can be partitioned as ["aa", "b"].
    • "aa" is a palindrome and "b" is a palindrome.
    • Only 1 cut is needed between "aa" and "b".
    • Therefore, minimum cuts needed = 1.
Example 2
  • Input: s = "abcde"
  • Output: 4
  • Explanation:
    • The string "abcde" has no palindromic substrings of length > 1.
    • Each character must be separated: ["a", "b", "c", "d", "e"].
    • This requires 4 cuts to separate all characters.
    • Therefore, minimum cuts needed = 4.
Constraints
  • 1 ≤ s.length ≤ 2000
  • s consists of lowercase English letters only
  • Time Complexity: O(n^2)
  • Space Complexity: O(n^2)
StringsDynamic Programming KPMGPhillips
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 dynamic programming to precompute palindrome information
  • Create a 2D boolean array to store if substring s[i..j] is palindrome
  • Use another DP array to store minimum cuts needed for substring s[0..i]
  • For each position, try all possible cuts and find minimum
  • If s[0..i] is already palindrome, no cuts needed

Steps to solve by this approach:

 Step 1: Create a 2D boolean array to precompute palindrome information for all substrings.
 Step 2: Fill the palindrome table using dynamic programming - single characters are palindromes.
 Step 3: For substrings of length 2 or more, check if first and last characters match and inner substring is palindrome.
 Step 4: Create a DP array to store minimum cuts needed for each prefix of the string.
 Step 5: For each position, if the entire prefix is palindrome, no cuts needed.
 Step 6: Otherwise, try all possible cut positions and find the minimum cuts required.
 Step 7: Return the minimum cuts needed for the entire string.

Submitted Code :