Ctrl K

Split Into Patterned Palindromes

Hard
Call a string of length m a **patterned palindrome** if there is a step d with 1 ≤ d < m and m divisible by d such that each of the d letter groups (positions r, r + d, r + 2d, … for r = 0, 1, …, d − 1) reads the same forwards and backwards. For example, "xyxy" is a patterned palindrome with step 2 (groups "xx" and "yy"). A single letter is never a patterned palindrome, since no step fits it. You are given a string text and an integer parts. Cut text into exactly parts non-empty consecutive pieces and change as few letters as possible so that every piece becomes a patterned palindrome. Return that smallest number of changes.
Example 1
text="pqpzz", parts=2 → 0

Cut into "pqp" and "zz": both are plain palindromes (step 1), so nothing has to change.

Example 2
text="xyxy", parts=1 → 0

The string itself is not a palindrome, but with step 2 the groups are "xx" and "yy", both palindromes.

Example 3
text="abcd", parts=2 → 2

The only split is "ab" and "cd"; a piece of length 2 only allows step 1 and needs one letter change. That is 2 in total.

Constraints
  • 2 ≤ len(text) ≤ 200
  • 1 ≤ parts ≤ len(text) / 2
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirror Chunk Split HardDisjoint Long Palindromes HardLongest Mirror Fragment MediumHidden in Order EasyCounting Palindromic Pieces of a String Medium
loading Python…
Input
text="pqpzz", parts=2
Expected
0

Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.