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.text="pqpzz", parts=2 → 0
Cut into "pqp" and "zz": both are plain palindromes (step 1), so nothing has to change.
text="xyxy", parts=1 → 0
The string itself is not a palindrome, but with step 2 the groups are "xx" and "yy", both palindromes.
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.
text consists of lowercase English letterstext="pqpzz", parts=2
0
Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.