Minimum Operations to Transform String - Solution & Explanation
Problem Statement
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
cin the string and replace every occurrence ofcwith 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:
- Change
'y'to'z'to get"zz". - Change
'z'to'a'to get"aa". - Thus, the answer is 2.
Example 2:
Input: s = "a"
Output: 0
Explanation:
- The string
"a"only consists of'a'āāāāāāā characters. Thus, the answer is 0.
Constraints:
1 <= s.length <= 5 * 105sconsists only of lowercase English letters.
Approach Overview
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.
Solution
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).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| 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 |
Video Solution
Minimum Operations to Transform String | Leetcode 3675 | Weekly Contest 466 ⢠Sanyam IIT Guwahati ⢠383 views views
Watch 9 more video solutions āFrequently Asked Questions
Is Minimum Operations to Transform String easy or hard?
Minimum Operations to Transform String Python/Java solution
How to solve Minimum Operations to Transform String in O(n)?
What is the best approach for Minimum Operations to Transform String?
Is Minimum Operations to Transform String asked at Google/Amazon/Meta?
What data structure is used in Minimum Operations to Transform String?
What is the time complexity of Minimum Operations to Transform String?
Ready to solve this problem?
Practice Minimum Operations to Transform String with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor