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-palindromes INPUT String s = "abcabc" a b c a b c 0 1 2 3 4 5 k = 2 (partitions) Possible partition: "abc" "abc" Both are semi-palindromes (divisor d=3: a|b|c) Semi-palindrome check: positions 0,3 | 1,4 | 2,5 match ALGORITHM STEPS (DP) 1 Precompute Costs cost[i][j] = changes for s[i..j] to be semi-palindrome 2 Check Divisors For each d dividing len, compare s[i], s[i+d], s[i+2d]... 3 DP Transition dp[i][j] = min changes for s[0..i] with j partitions 4 Find Minimum Return dp[n][k] DP Table (simplified) j=1 j=2 i=3 i=6 0 - 0 0 "abc" + "abc" = 0 changes FINAL RESULT Output 0 Optimal Partition: "abc" "abc" Verification: "abc" with d=3: pos 0,3 --> a,a [OK] Semi-palindrome! "abc" with d=3: pos 0,3 --> a,a [OK] Semi-palindrome! Total changes: 0 + 0 = 0 Key Insight: A string is a semi-palindrome if we can find a divisor d of its length where characters at positions i, i+d, i+2d, ... form a palindrome for each starting position i from 0 to d-1. The DP approach precomputes costs for all substrings, then finds optimal k-way partition with minimum total changes. TutorialsPoint - Minimum Changes to Make K Semi-palindromes | Dynamic Programming Approach
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