Cleanest Java Code. Only one if-else. Greedy. Simple Intuition. Please Upvote.
Intuition\n Describe your first thoughts on how to solve this problem. \nGreedy is the way to go.\nMain reason for this is that we want the smalles lexicographi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGreedy is the way to go.\nMain reason for this is that we want the smalles lexicographical string. Therefore we want to invest as much as we can on the earlier letters. \nAs even bzzzz is smaller than caaaa\n\nTry to make every letter starting from the first in the string to \'a\'. If you can\'t then try to get as close to a.\n\nTo handle the circular nature of the letter, just add a check for which is less. Going to a (0) or going to z+1 (26).\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nChange the string to char array. Then just minimize the chars as the budget allows.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N) because String manipulation in Java is jank.\nIf the question provided a char array this could have been done in O(1);\n# Code\n```\nclass Solution {\n    public String getSmallestString(String s, int k) {\n        char[] chars = s.toCharArray();\n        int i = 0;\n        while(k>0 && i <s.length()){\n            int distance = Math.min(chars[i] - \'a\', \'z\' - chars[i] + 1);\n            if(distance > k){\n                chars[i] = (char) (chars[i] - k);  \n            } else {\n                chars[i] = \'a\';\n            }\n            k -= distance;\n            i++;\n        }\n        return new String(chars);\n    }\n}\n```
['Java']