Minimum Character Changes to Turn a Password into a Palindrome

Quick Overview

Find the fewest single-character replacements that turn a lowercase password string into a palindrome. The problem tests reasoning about mirrored positions in a string and writing a two-pointer scan that runs in linear time with constant extra space.

Minimum Character Changes to Turn a Password into a Palindrome

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a string `password` consisting of lowercase English letters. In one operation, you can change any single character to any other lowercase English letter. Return the minimum number of operations required to transform `password` into a palindrome. A string is a palindrome if it reads the same forward and backward. ### Function Signature ```python def min_operations(password: str) -> int: ``` ### Constraints - `1 <= len(password) <= 10^5` - `password` consists of lowercase English letters only. ### Examples ```text Input: password = "abcda" Output: 1 ``` The outer pair `('a', 'a')` already matches and `'c'` is the middle character. Only the pair `('b', 'd')` differs, and changing index 1 to `'d'` or index 3 to `'b'` gives a palindrome ("adcda" or "abcba") in one operation. ```text Input: password = "abcde" Output: 2 ``` The pairs `('a', 'e')` and `('b', 'd')` both differ and `'c'` is the middle character. Each mismatched pair needs one change, so 2 operations are needed. ```text Input: password = "abba" Output: 0 ``` The string already reads the same in both directions, so no change is needed.

Overview: Find the fewest single-character replacements that turn a lowercase password string into a palindrome. The problem tests reasoning about mirrored positions in a string and writing a two-pointer scan that runs in linear time with constant extra space.

Citadel logo
Citadel
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a string password consisting of lowercase English letters. In one operation, you can change any single character to any other lowercase English letter.

Return the minimum number of operations required to transform password into a palindrome. A string is a palindrome if it reads the same forward and backward.

Function Signature

def min_operations(password: str) -> int:

Constraints

  • 1 <= len(password) <= 10^5
  • password consists of lowercase English letters only.

Examples

Input: password = "abcda"
Output: 1

The outer pair ('a', 'a') already matches and 'c' is the middle character. Only the pair ('b', 'd') differs, and changing index 1 to 'd' or index 3 to 'b' gives a palindrome ("adcda" or "abcba") in one operation.

Input: password = "abcde"
Output: 2

The pairs ('a', 'e') and ('b', 'd') both differ and 'c' is the middle character. Each mismatched pair needs one change, so 2 operations are needed.

Input: password = "abba"
Output: 0

The string already reads the same in both directions, so no change is needed.