Palindrome Partitioning III - Problem
You are given a string s containing lowercase letters and an integer k.
You need to:
- First, change some characters of
sto other lowercase English letters. - Then divide
sintoknon-empty disjoint substrings such that each substring is a palindrome.
Return the minimal number of characters that you need to change to divide the string.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abc", k = 2
›
Output:
1
💡 Note:
Change one character to create palindromes: "a" | "bb" (change 'c' to 'b') or "aa" | "c" (change 'b' to 'a'). Minimum cost is 1.
Example 2 — Already Palindromic
$
Input:
s = "aabaa", k = 3
›
Output:
0
💡 Note:
Can partition as "a" | "aba" | "a" with no changes needed since each part is already palindromic.
Example 3 — Single Part
$
Input:
s = "leetcode", k = 1
›
Output:
3
💡 Note:
Need to make entire string palindromic. Change positions to match: "leeecode" → "leeeeeel" requires 3 changes minimum.
Constraints
- 1 ≤ k ≤ s.length ≤ 100
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input
String "abc" with k=2 parts needed
2
Process
Find optimal way to partition and make palindromes
3
Output
Minimum 1 change needed
Key Takeaway
🎯 Key Insight: Precompute all substring palindrome costs, then use DP to find optimal k-way partition
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code