Palindrome Blocks
MediumStrings ยท Citadel
Given a lowercase string s and an integer k, split s into consecutive blocks of length k; the last block is shorter if k does not divide the length of s.
Return the minimum number of single-character replacements so that every block is a palindrome and all full blocks are identical to each other. A shorter last block only has to be a palindrome by itself.
Example 1
- Input:
- s = "abcxabdcbaxy", k = 4
- Output:
- 8
- Explanation
- Positions 0 and 3 of all three blocks form one group of six letters where
aappears twice, so 4 replacements; positions 1 and 2 also need 4.
Example 2
- Input:
- s = "abcabcab", k = 3
- Output:
- 3
- Explanation
- Outer positions of the two full blocks cost 2, their middles cost 0, and the last block
abcosts 1.
Example 3
- Input:
- s = "aabbb", k = 1
- Output:
- 2
Constraints
1 <= s.length <= 10^5scontains only lowercase English letters1 <= k <= 10^5