Minimum Changes for Palindromic Blocks
Problem statement
You are given a lowercase string password and a positive integer k. Partition the string into consecutive, non-overlapping blocks of k characters, starting at index 0. The last block may have fewer than k characters.
Change as few characters as possible so that every block is a palindrome. Blocks are independent and do not need to become equal to one another. Return the minimum number of changed positions.
Function
minChangesForPalindromicBlocks(password: String, k: int) → intExamples
Example 1
password = "abcaabba"k = 4return = 1The blocks are abca and abba. The second is already palindromic; changing either b or c in the first block is sufficient.
Example 2
password = "abcdef"k = 3return = 2The blocks abc and def each have one mismatched mirrored pair, so each needs one change.
Example 3
password = "aaaa"k = 1return = 0Every one-character block is already a palindrome.
Example 4
password = "abcde"k = 3return = 2The blocks are abc and de. Each has one mismatched mirrored pair, so the total is 2.
Constraints
- 1 <= password.length <= 200000
- 1 <= k <= password.length
- password contains only lowercase English letters.
- The final block may have fewer than k characters.