[LeetCode] Minimum Operations to Transform String

3675. Minimum Operations to Transform String

You are given a string s consisting only of lowercase English letters.

You can perform the following operation any number of times (including zero):

  • Choose any character c in the string and replace every occurrence of c with the next lowercase letter in the English alphabet.

Return the minimum number of operations required to transform s into a string consisting of only 'a' characters.

Note:Consider the alphabet as circular, thus 'a' comes after 'z'.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int minOperations(string s) {
int res = 0;
for(auto& ch : s) {
if(ch != 'a') res = max(res, 26 - ch + 'a');
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/minimum-operations-to-transform-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.