Problem · String
Minimum Changes for Palindromic Blocks
Learn this problemProblem statement
You are given a lowercase string password and a positive integer k. The string length is divisible by k, so it is partitioned into consecutive, non-overlapping blocks of exactly 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.
Constraints
1 <= password.length <= 2000001 <= k <= password.lengthpassword.length % k == 0passwordcontains only lowercase English letters.