Minimize Changes to Make Every Password Block Palindromic
Quick Overview
Return the minimum character changes needed so every fixed-length block in a lowercase password becomes a palindrome while block boundaries remain unchanged.
Minimize Changes to Make Every Password Block Palindromic
Company: Citadel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: HR Screen
## Problem
A password is partitioned from left to right into consecutive blocks of exactly `k` characters. Change the minimum number of characters so that every block is a palindrome.
Changing one position to any lowercase English letter costs `1`. Return only the minimum number of changed positions.
### Function Contract
Implement `minimumPalindromeBlockChanges(password, k)` and return an integer.
### Constraints & Assumptions
- `1 <= len(password) <= 200,000`.
- `1 <= k <= len(password)` and `len(password)` is divisible by `k`.
- `password` contains only lowercase English letters.
- Blocks are `[0, k)`, `[k, 2k)`, and so on; characters are never moved between blocks.
- A one-character block is already a palindrome.
### Clarifying Questions to Ask
- Must the blocks all become the same palindrome? No; each block is independent.
- Can characters be inserted or deleted? No, only replaced.
- Does changing both characters in one mirrored pair ever help? No; one replacement can make unequal mirrored characters match.
- Is the final partial block possible? No, the length is a multiple of `k`.
```hint Count disagreements, not character frequencies
Within each block, inspect mirrored positions from the ends toward the center. Each unequal pair needs exactly one replacement.
```
### Examples
- `password = "abcaabba"`, `k = 4` returns `1`: `abca` needs one change, while `abba` needs none.
- `password = "abcdef"`, `k = 3` returns `2`: both `abc` and `def` contain one unequal mirrored pair.
- `password = "aaaa"`, `k = 1` returns `0`.
### Evaluation Focus
- Resets mirrored comparisons at every block boundary.
- Counts one change for each unequal mirrored pair and ignores a middle character in odd-length blocks.
- Runs in `O(n)` time and `O(1)` auxiliary space.
### Extensions to Discuss
1. How would weighted replacement costs change the calculation?
2. What if all blocks had to become the same palindrome?
3. How would you return one lexicographically smallest repaired password as well as the count?
Quick Answer: Return the minimum character changes needed so every fixed-length block in a lowercase password becomes a palindrome while block boundaries remain unchanged.
A password is partitioned from left to right into consecutive blocks of exactly k characters. Change the minimum number of characters so that every block is a palindrome.
Changing one position to any lowercase English letter costs 1. Return only the minimum number of changed positions.
Function Contract
Implement minimumPalindromeBlockChanges(password, k) and return an integer.
Constraints & Assumptions
1 <= len(password) <= 200,000
.
1 <= k <= len(password)
and
len(password)
is divisible by
k
.
password
contains only lowercase English letters.
Blocks are
[0, k)
,
[k, 2k)
, and so on; characters are never moved between blocks.
A one-character block is already a palindrome.
Clarifying Questions to Ask Guidance
Must the blocks all become the same palindrome? No; each block is independent.
Can characters be inserted or deleted? No, only replaced.
Does changing both characters in one mirrored pair ever help? No; one replacement can make unequal mirrored characters match.
Is the final partial block possible? No, the length is a multiple of
k
.
Examples
password = "abcaabba"
,
k = 4
returns
1
:
abca
needs one change, while
abba
needs none.
password = "abcdef"
,
k = 3
returns
2
: both
abc
and
def
contain one unequal mirrored pair.
password = "aaaa"
,
k = 1
returns
0
.
Evaluation Focus
Resets mirrored comparisons at every block boundary.
Counts one change for each unequal mirrored pair and ignores a middle character in odd-length blocks.
Runs in
O(n)
time and
O(1)
auxiliary space.
Extensions to Discuss
How would weighted replacement costs change the calculation?
What if all blocks had to become the same palindrome?
How would you return one lexicographically smallest repaired password as well as the count?