Problem · String

Minimum Changes for Palindromic Blocks

Learn this problem
EasyCitadel logoCitadelINTERNOA

Problem 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) → int

Examples

Example 1

password = "abcaabba"k = 4return = 1

The 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 = 2

The blocks abc and def each have one mismatched mirrored pair, so each needs one change.

Example 3

password = "aaaa"k = 1return = 0

Every one-character block is already a palindrome.

Constraints

  • 1 <= password.length <= 200000
  • 1 <= k <= password.length
  • password.length % k == 0
  • password contains only lowercase English letters.

More Citadel problems

drafts saved locally
public int minChangesForPalindromicBlocks(String password, int k) {
  // write your code here
}
password"abcaabba"
k4
expected1
checking account