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.