✅ Beats 100 % || Time O(n) || Space O(n) || Java - 0 ms || Easy Explained ✅
\n# Intuition\nThe problem requires us to create the lexicographically smallest string of length k by using the characters from the given string s. To achieve t
![image.png](https://assets.leetcode.com/users/images/c4a7b1fc-0fcb-46f0-b0dd-f8e84921a283_1712469754.9881017.png)\n# Intuition\nThe problem requires us to create the lexicographically smallest string of length `k` by using the characters from the given string `s`. To achieve this, we need to manipulate the characters in `s` such that the resulting string is the smallest possible.\n\n# Approach\n1. Convert the given string `s` into a character array `t`.\n2. Iterate through each character in the array `t`.\n3. For each character:\n   - Calculate the distance to character \'a\' using the `distToA` method. This distance represents the number of steps required to change the character to \'a\' in the alphabet. If the character is \'a\', the distance is 0; otherwise, it is the minimum of the difference between the character and \'a\' and the difference between \'z\' and the character plus 1.\n   - If the remaining length `k` is greater than or equal to the current distance, decrement `k` by the current distance and set the character to \'a\'.\n   - If `k` is less than the current distance, we need to decrement the character value by `k` steps. If the resulting character is still within the range [\'a\', \'z\'], set the character to the new value. Otherwise, adjust the character to wrap around the alphabet (\'z\' to \'a\') and subtract the remaining steps from \'z\'.\n4. After processing all characters, construct a string from the modified character array and return it as the result.\n\n# Complexity\n- Time complexity: \n  - The algorithm iterates through each character in the input string once. Each iteration involves constant-time operations. Thus, the time complexity is O(n), where n is the length of the input string.\n- Space complexity:\n  - The algorithm uses extra space to store the character array representation of the input string. Therefore, the space complexity is O(n), where n is the length of the input string.\n\n# Code\n```java\nclass Solution {\n    public String getSmallestString(String s, int k) {\n        char[] t = s.toCharArray();\n\n        for (int i = 0; i < t.length; i++) {\n            int currentDist = distToA(t[i]);\n\n            if (k >= currentDist) {\n                k -= currentDist;\n                t[i] = \'a\';\n            } else {\n                if (k > 0) {\n                    if (t[i] - k >= \'a\') {\n                        t[i] = (char) (t[i] - k);\n                    } else {\n                        t[i] = (char) (\'z\' - (k - 1));\n                    }\n                    k = 0;\n                }\n            }\n        }\n        return new String(t);\n    }\n\n    private int distToA(char c) {\n        return Math.min(c - \'a\', 26 - (c - \'a\'));\n    }\n}\n```
['Java']