Problem Description
Given a string s and an integer k, you can change any letter in s to any other lowercase letter any number of times. Changing a letter incurs a cost defined as the minimum distance between the two letters on a cyclic alphabet (i.e. the distance from 'a' to 'z' is 1). The task is to find a string t — lexicographically smallest possible — such that the sum of costs to change s into t does not exceed k.
Key Insights
- The cost to change a letter from x to y is given by: cost = min(|x - y|, 26 - |x - y|).
- To obtain the lexicographically smallest result, try to change each character in s to the smallest possible letter (starting from 'a') if the cost is within the remaining budget.
- Since you are allowed to not spend the entire available budget, you always have the option of leaving a character unchanged (cost 0).
- Process the string from left to right and for each position, search for the smallest candidate letter (strictly smaller than the current letter) that can be achieved with the remaining k; if none is possible, leave the letter unchanged.
Space and Time Complexity
Time Complexity: O(n * 26) where n is the length of the string (n ≤ 100).
Space Complexity: O(n) for storing the result (or O(1) if done in-place).
Solution
The solution uses a greedy approach. For each character in the string (from left to right), we iterate over the candidate letters starting from 'a' up to the character just before the current character (because a candidate equal to the current letter produces no lexicographic improvement). For each candidate letter, we calculate the cyclic distance between the current character and the candidate. If the cost does not exceed the remaining budget k, we choose that candidate, reduce k by the cost, and move to the next character. If no candidate letter (that is lexicographically smaller) can be chosen within the budget, we leave the character unchanged.
Data structures used are simple variables (for the result and k) and the algorithm relies on iterative greedy decisions based on the cost function.