Problem · String

Minimum Changes for a Periodic Palindrome

Learn this problem
MediumCitadelOA

Problem statement

The current password is a string currentPassword consisting only of lowercase Latin letters. Create a string newPassword of the same length that satisfies both of these requirements:

  • newPassword is a palindrome.
  • newPassword has a period of k; that is, newPassword[i] = newPassword[i + k] for every 1 ≤ i ≤ length(newPassword) - k.

Return the minimum number of characters that must be changed in currentPassword to create a valid newPassword.

Function

findMinChanges(currentPassword: String, k: int) → int

Examples

Example 1

currentPassword = "abzzbz"k = 3return = 1

Changing the first character of currentPassword to z produces zbzzbz, which is a palindrome with a period of 3. Therefore, the answer is 1.

Constraints

  • 1 ≤ k < length(currentPassword) ≤ 2 × 10^5
  • currentPassword contains only lowercase Latin letters.
  • length(currentPassword) is divisible by k.

More Citadel problems

drafts saved locally
public int findMinChanges(String currentPassword, int k) {
  // write your code here
}
currentPassword"abzzbz"
k3
expected1
checking account