Minimum Operations for a Palindrome with Left Rotations
Quick Overview
Minimize one-letter cyclic increments and one-position left rotations needed to make a lowercase string a palindrome, allowing the operations in any order.
Minimum Operations for a Palindrome with Left Rotations
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
# Minimum Operations for a Palindrome with Left Rotations
You are given a string `s` of lowercase English letters. Each operation is one of:
- Increment the letter at any one position to the next lowercase letter, with `'z'` wrapping to `'a'`.
- Rotate the string left by one position, moving its first character to the end.
You may use either operation any number of times and in any order. Return the minimum total number of operations needed to make the string a palindrome.
### Function
`int minOperations(string s)`
### Example 1
**Input:** `s = "ddm"`
**Output:** `1`
**Explanation:** Rotate left once to obtain `"dmd"`, which is a palindrome. The original string is not a palindrome, so zero operations cannot suffice.
### Example 2
**Input:** `s = "zc"`
**Output:** `3`
**Explanation:** Increment the first character three times: `"zc" -> "ac" -> "bc" -> "cc"`. Rotating a two-character string only swaps its two letters and cannot reduce the number of increments needed to make them equal.
### Constraints
- `2 <= s.length <= 2000`
- `s` contains only lowercase English letters.
### Clarifications
- Each one-letter increment and each one-position left rotation costs one operation.
- A decrement or a one-step right rotation is not an allowed operation.
- You may increment either character of a pair and may also increment both.
- An already-palindromic string needs `0` operations.
### What a Strong Answer Covers
- Counting both rotation and letter-change operations.
- Handling alphabet wraparound using only permitted forward increments.
- Finding the minimum over interleaved operation sequences at the full length limit.
Overview: Minimize one-letter cyclic increments and one-position left rotations needed to make a lowercase string a palindrome, allowing the operations in any order.
Minimum Operations for a Palindrome with Left Rotations
Google
mediumSoftware EngineerOtherCoding & Algorithms
1
0
Minimum Operations for a Palindrome with Left Rotations
You are given a string s of lowercase English letters. Each operation is one of:
Increment the letter at any one position to the next lowercase letter, with
'z'
wrapping to
'a'
.
Rotate the string left by one position, moving its first character to the end.
You may use either operation any number of times and in any order. Return the minimum total number of operations needed to make the string a palindrome.
Function
int minOperations(string s)
Example 1
Input:s = "ddm"
Output:1
Explanation: Rotate left once to obtain "dmd", which is a palindrome. The original string is not a palindrome, so zero operations cannot suffice.
Example 2
Input:s = "zc"
Output:3
Explanation: Increment the first character three times: "zc" -> "ac" -> "bc" -> "cc". Rotating a two-character string only swaps its two letters and cannot reduce the number of increments needed to make them equal.
Constraints
2 <= s.length <= 2000
s
contains only lowercase English letters.
Clarifications
Each one-letter increment and each one-position left rotation costs one operation.
A decrement or a one-step right rotation is not an allowed operation.
You may increment either character of a pair and may also increment both.
An already-palindromic string needs
0
operations.
What a Strong Answer Covers Guidance
Counting both rotation and letter-change operations.
Handling alphabet wraparound using only permitted forward increments.
Finding the minimum over interleaved operation sequences at the full length limit.