Watch 10 video solutions for Lexicographically Smallest String After Operations With Constraint, a medium level problem involving String, Greedy. This walkthrough by Aryan Mittal has 4,375 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s and an integer k.
Define a function distance(s1, s2) between two strings s1 and s2 of the same length n as:
s1[i] and s2[i] when the characters from 'a' to 'z' are placed in a cyclic order, for all i in the range [0, n - 1].For example, distance("ab", "cd") == 4, and distance("a", "z") == 1.
You can change any letter of s to any other lowercase English letter, any number of times.
Return a string denoting the lexicographically smallest string t you can get after some changes, such that distance(s, t) <= k.
Example 1:
Input: s = "zbbz", k = 3
Output: "aaaz"
Explanation:
Change s to "aaaz". The distance between "zbbz" and "aaaz" is equal to k = 3.
Example 2:
Input: s = "xaxcd", k = 4
Output: "aawcd"
Explanation:
The distance between "xaxcd" and "aawcd" is equal to k = 4.
Example 3:
Input: s = "lol", k = 0
Output: "lol"
Explanation:
It's impossible to change any character as k = 0.
Constraints:
1 <= s.length <= 1000 <= k <= 2000s consists only of lowercase English letters.Problem Overview: You are given a lowercase string s and an integer k. Each operation can shift a character forward or backward in the alphabet with wrap-around (cyclic). The goal is to produce the lexicographically smallest possible string while keeping the total shift cost โค k.
Approach 1: Greedy Approach Using Character Replacement (O(n) time, O(1) space)
The key observation is that lexicographic order is determined from left to right. Reducing earlier characters has a much bigger impact than modifying later ones. Iterate through the string and try to convert each character to 'a'. Compute the cyclic distance between the current character and 'a'. If the required cost is โค k, convert the character directly to 'a' and subtract that cost from k. If the cost is larger than the remaining budget, reduce the character by exactly k steps toward 'a' and stop further reductions since the budget becomes zero.
This works because any extra reduction on later characters cannot produce a lexicographically smaller result than minimizing earlier characters first. The algorithm performs a single pass through the string and constant-time arithmetic per character, giving O(n) time and O(1) space.
Approach 2: Cyclic Distance and Replacement Strategy (O(n) time, O(1) space)
This approach explicitly models the alphabet as a cycle of size 26. For each character, compute the minimal cyclic distance to 'a' using min(c - 'a', 26 - (c - 'a')). If this minimal distance is within the remaining operation budget, replace the character with 'a'. Otherwise, partially move the character toward 'a' by exactly k positions in the backward direction and terminate since the budget is exhausted.
The cyclic distance calculation is important for cases like 'z', where wrapping forward by one step reaches 'a'. By always selecting the smallest cyclic move that minimizes the character value, the algorithm guarantees the smallest possible prefix at every step. The logic relies on simple arithmetic rather than simulation, which keeps the solution linear.
This problem mainly combines ideas from string manipulation and greedy algorithms. The greedy choice is safe because reducing earlier characters always dominates improvements later in the string.
Recommended for interviews: The greedy single-pass solution is what interviewers expect. A brute-force exploration of possible shifts would be exponential and unnecessary. Showing the greedy reasoningโminimizing each character from left to right while tracking the remaining budgetโdemonstrates strong understanding of lexicographic ordering and greedy optimization.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Character Replacement | O(n) | O(1) | Best general solution. Minimizes characters from left to right using remaining operation budget. |
| Cyclic Distance Strategy | O(n) | O(1) | Useful when reasoning about alphabet wrap-around and minimal cyclic shifts. |