Minimum Changes to Make K Semi-palindromes - Problem

Given a string s and an integer k, partition s into k substrings such that the letter changes needed to make each substring a semi-palindrome are minimized.

Return the minimum number of letter changes required.

A semi-palindrome is a special type of string that can be divided into palindromes based on a repeating pattern. To check if a string is a semi-palindrome:

  • Choose a positive divisor d of the string's length. d can range from 1 up to, but not including, the string's length.
  • For a string of length 1, it does not have a valid divisor as per this definition, since the only divisor is its length, which is not allowed.
  • For a given divisor d, divide the string into groups where each group contains characters from the string that follow a repeating pattern of length d.
  • Specifically, the first group consists of characters at positions 1, 1+d, 1+2d, ...; the second group includes characters at positions 2, 2+d, 2+2d, ... etc.
  • The string is considered a semi-palindrome if each of these groups forms a palindrome.

Example: Consider the string "abcabc" with length 6. Valid divisors are 1, 2, and 3. For d = 3: Group 1 (positions 1,4): "aa", Group 2 (positions 2,5): "bb", Group 3 (positions 3,6): "cc". All groups form palindromes, so "abcabc" is a semi-palindrome.

Input & Output

Example 1 — Perfect Semi-palindromes
$ Input: s = "abcabc", k = 2
Output: 0
💡 Note: Partition into "abc" and "abc". Both are already semi-palindromes with d=3: groups (a,a), (b,b), (c,c) are all palindromes. Cost = 0 + 0 = 0
Example 2 — Need Changes
$ Input: s = "abcdef", k = 2
Output: 2
💡 Note: Best partition is "abc" | "def". For "abc" with d=1: need to change 'a'→'c' or 'c'→'a' (1 change). For "def" with d=1: need to change 'd'→'f' or 'f'→'d' (1 change). Total = 1 + 1 = 2
Example 3 — Single Characters
$ Input: s = "abc", k = 3
Output: 0
💡 Note: Partition into "a", "b", "c". Single characters have no valid divisors, so cost is 0 for each. Total = 0 + 0 + 0 = 0

Constraints

  • 1 ≤ s.length ≤ 200
  • 1 ≤ k ≤ s.length
  • s consists only of lowercase English letters

Visualization

Tap to expand
Minimum Changes to Make K Semi-palindromesInput: s = "abcabc", k = 2Split here"abc""abc"Semi-palindrome (d=3)Semi-palindrome (d=3)Groups: (a,a), (b,b), (c,c)Groups: (a,a), (b,b), (c,c)Cost: 0 changesCost: 0 changesTotal Cost: 0 + 0 = 0Output: 0
Understanding the Visualization
1
Input
String 'abcabc' needs to be split into k=2 parts
2
Partition
Find optimal way to split: 'abc' | 'abc'
3
Check
Each part is already a semi-palindrome, total cost = 0
Key Takeaway
🎯 Key Insight: Pre-compute all substring costs and use DP to find the optimal partitioning strategy
Asked in
Google 12 Microsoft 8 Amazon 6
12.0K Views
Medium Frequency
~35 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen