|
| 1 | +--- |
| 2 | +id: Lexicographically-Smallest-String-After-Operations-With-Constraint |
| 3 | +title: Lexicographically Smallest String After Operations With Constraint |
| 4 | +sidebar_label: 3106. Lexicographically Smallest String After Operations With Constraint |
| 5 | +tags: |
| 6 | +- Java |
| 7 | +- Greedy |
| 8 | +- String |
| 9 | +description: "This document provides a solution where we Return a string denoting the |
| 10 | +lexicographically smallest |
| 11 | + string t" |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem |
| 15 | + |
| 16 | +You are given a string `s` and an integer `k`. |
| 17 | + |
| 18 | +Define a function `distance(s1, s2)` between two strings `s1` and `s2` of the same length `n` as: |
| 19 | + |
| 20 | +The **sum** of the **minimum distance** between `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]`. |
| 21 | +For example, `distance("ab", "cd") == 4`, and `distance("a", "z") == 1`. |
| 22 | + |
| 23 | +You can **change** any letter of `s` to **any** other lowercase English letter, **any** number of times. |
| 24 | + |
| 25 | +Return a string denoting the |
| 26 | +lexicographically smallest |
| 27 | +string `t` you can get after some changes, such that `distance(s, t) <= k`. |
| 28 | + |
| 29 | +### Examples |
| 30 | + |
| 31 | +**Example 1:** |
| 32 | + |
| 33 | +**Input:** `s = "zbbz"`, `k = 3` |
| 34 | + |
| 35 | +**Output:** "aaaz" |
| 36 | + |
| 37 | +**Explanation:** |
| 38 | + |
| 39 | +Change s to "aaaz". The distance between "zbbz" and "aaaz" is equal to `k = 3`. |
| 40 | + |
| 41 | +**Example 2:** |
| 42 | + |
| 43 | +**Input:** `s = "xaxcd"`, `k = 4` |
| 44 | + |
| 45 | +**Output:** "aawcd" |
| 46 | + |
| 47 | +**Explanation:** |
| 48 | + |
| 49 | +The distance between "xaxcd" and "aawcd" is equal to `k = 4`. |
| 50 | + |
| 51 | + |
| 52 | +### Constraints |
| 53 | + |
| 54 | +- $1 <= s.length <= 100$ |
| 55 | +- $0 <= k <= 2000$ |
| 56 | +- `s` consists only of lowercase English letters. |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Approach |
| 61 | + |
| 62 | +## 1. Conversion to Character Array: |
| 63 | + |
| 64 | +char[] `result = s.toCharArray()`;: This line converts the input string s into a character array named result. This array will hold the modified string. |
| 65 | + |
| 66 | +## 2. Iterating Through Characters: |
| 67 | + |
| 68 | +The function uses an outer for loop to iterate through each character i in the original string s. |
| 69 | + |
| 70 | +## 3. Finding the Best Replacement Character: |
| 71 | + |
| 72 | +An inner for loop iterates through all lowercase letters `char c = 'a'; c <= 'z'; c++`. |
| 73 | + |
| 74 | +`Inside the inner loop:` |
| 75 | +- **distance(s.charAt(i), c):** |
| 76 | +This calculates the distance between the current character s[i] and the candidate replacement character c using a separate distance function (assumed to be defined elsewhere). |
| 77 | + |
| 78 | +- **if `(distance(s.charAt(i), c) <= k):`** This checks if changing the current character to c is allowed within the k limit based on the distance. |
| 79 | + |
| 80 | +**If the distance is less than or equal to k:** |
| 81 | +- `result[i] = c`;: The character in the result array at position i is updated with the new character c. |
| 82 | +- `k -= distance(s.charAt(i), c)`;: The remaining allowed changes (k) are decremented by the distance used for this replacement. |
| 83 | +- break;: The inner loop exits as a suitable replacement has been found within the limit. |
| 84 | + |
| 85 | +## 4. Returning the Modified String: |
| 86 | + |
| 87 | +return new String(result);: This line converts the modified character array result back into a String object and returns it. |
| 88 | + |
| 89 | +## Solution |
| 90 | + |
| 91 | +#### Code in Java |
| 92 | + |
| 93 | +```java |
| 94 | +class Solution { |
| 95 | + public int distance(char s1, char s2) { |
| 96 | + int diff = Math.abs(s1 - s2); |
| 97 | + return Math.min(diff, 26 - diff); |
| 98 | + } |
| 99 | + public String getSmallestString(String s, int k) { |
| 100 | + char[] result = s.toCharArray(); |
| 101 | + for (int i = 0; i < s.length(); i++) { |
| 102 | + for (char c = 'a'; c <= 'z'; c++) { |
| 103 | + if (distance(s.charAt(i), c) <= k) { |
| 104 | + result[i] = c; |
| 105 | + k -= distance(s.charAt(i), c); |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return new String(result); |
| 111 | + |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Complexity Analysis |
| 117 | + |
| 118 | +#### Time Complexity: $O(n * k)$ |
| 119 | + |
| 120 | +> **Reason**: In the worst case, the inner loop runs completely for every character in the outer loop (if all characters need to be replaced). This leads to a total of n * k iterations. |
| 121 | +
|
| 122 | +#### Space Complexity: $O(n)$ |
| 123 | + |
| 124 | +> **Reason**: The space complexity of the provided code is dominated by the character array result used to store the modified string. |
| 125 | +
|
| 126 | +# References |
| 127 | + |
| 128 | +- **LeetCode Problem:** [Lexicographically Smallest String After Operations With Constraint](https://leetcode.com/problems/lexicographically-smallest-string-after-operations-with-constraint/description/) |
| 129 | +- **Solution Link:** [Lexicographically Smallest String After Operations With Constraint on LeetCode](https://leetcode.com/problems/lexicographically-smallest-string-after-operations-with-constraint/solutions/) |
0 commit comments