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'.
Example 1:
Input: s = "yz"
Output: 2
Explanation:
'y' to 'z' to get "zz".'z' to 'a' to get "aa".Example 2:
Input: s = "a"
Output: 0
Explanation:
"a" only consists of 'a' characters. Thus, the answer is 0.
Constraints:
1 <= s.length <= 5 * 105s consists only of lowercase English letters.Problem Overview: You are given two strings and need the minimum number of operations required to transform the source string into the target string. Instead of fixing characters one by one, an operation can fix a contiguous segment, so the goal is to minimize how many such segments you apply.
Approach 1: Naive Simulation (O(n²) time, O(1) space)
A straightforward idea is to scan the string and try applying an operation starting at every mismatched index. For each position, expand forward while characters differ and simulate fixing that range. This repeatedly scans portions of the string, which leads to nested iteration. The approach works for small inputs but becomes inefficient because every mismatch can trigger another scan of the remaining substring.
Approach 2: Greedy Single Pass (O(n) time, O(1) space)
The optimal solution comes from a simple observation: a single operation can fix an entire continuous block of mismatched characters. Instead of fixing characters individually, count how many mismatch segments exist. Iterate through both strings once. When s[i] != t[i] and either i == 0 or the previous characters already matched, you have discovered the start of a new segment and must perform one operation. Continue scanning until the mismatch block ends.
This greedy approach works because extending an operation across an already mismatched block never increases the cost. Treat each contiguous mismatch region as one operation and count them during a single traversal. The algorithm only performs constant work per character, making it linear and memory efficient.
Implementation is straightforward: iterate with an index, compare characters, and increment a counter when a new mismatch block begins. This pattern appears frequently in string problems where operations affect contiguous ranges and the optimal strategy is to compress adjacent work into one step. The reasoning also follows classic greedy logic: make the locally optimal decision (start an operation at the first mismatch) and extend it as far as possible.
Recommended for interviews: Interviewers expect the greedy single-pass solution. The naive simulation demonstrates baseline reasoning, but recognizing that consecutive mismatches can be grouped into a single operation shows strong pattern recognition in string traversal problems.
According to the problem description, we always start from the character 'b' and successively change each character to the next one until it becomes 'a'. Therefore, we only need to find the character in the string that is farthest from 'a' and calculate its distance to 'a' to get the answer.
The time complexity is O(n), where n is the length of the string s. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Naive Simulation | O(n²) | O(1) | Conceptual baseline when first reasoning about mismatch segments |
| Greedy Single Pass | O(n) | O(1) | Optimal approach for large strings and typical interview solutions |
Minimum Operations to Transform String | Leetcode 3675 | Weekly Contest 466 • Sanyam IIT Guwahati • 295 views views
Watch 9 more video solutions →Practice Minimum Operations to Transform String with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor