Palindrome partitioning II

Dynamic Programming MCM DP Hard

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.

Examples:

Input : s = "aab"

Output : 1

Explanation : The palindrome partitioning ["aa", "b"] could be produced using 1 cut.

Input : s = "abaaba"

Output : 0

Explanation : The complete string can be considered as a partition as the string itself is palindrome.

There are other ways to partition the string but it requires more number of cuts.

Input : s = "abcd"

Constraints

  • 1 <= s.length <= 2000
  • s consist of only lowercase English letters

Hints

  • Define dp[i] as the minimum number of cuts needed to partition s[0:i] into palindromes. Check all possible palindrome substrings ending at i.
  • "Define isPalindrome[i][j] = True if s[i:j] is a palindrome. Use isPalindrome[i][j] = (s[i] == s[j] && isPalindrome[i+1][j-1]) for efficient checking."

Company Tags

Teladoc Health Philips Healthcare Pinterest Square Medtronic Unity Technologies Visa Cerner AMD Electronic Arts Uber Wayfair Stripe Epic Systems Swiggy Oracle Western Digital Boston Consulting Group Micron Technology Johnson & Johnson Texas Instruments IBM Ernst & Young Shopify Bain & Company Google Microsoft Amazon Meta Apple Netflix Adobe