Easy 🔥🔥 Python 🐍 // Java // Python3 // C++ 🔥🔥 Explained 💀☠️ Beats 💯
Intuition\n\n\n\n\n\n\n\n Describe your first thoughts on how to solve this problem. \nC++ []\nclass Solution {\npublic:\n    string getSmallestString(string s,
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/482013c8-798a-4e76-8350-bb09fc400560_1712463394.0955017.jpeg)\n\n![Screenshot 2024-04-07 093152.png](https://assets.leetcode.com/users/images/834621a5-c5f0-4939-8984-909bcb41fcfa_1712462561.1025996.png)\n![Screenshot 2024-04-07 101424.png](https://assets.leetcode.com/users/images/9d98d5c0-ca37-4491-aa1a-1914bf0457d6_1712465075.9939566.png)\n![Screenshot 2024-04-07 101730.png](https://assets.leetcode.com/users/images/a283a8eb-eb1c-4ace-8ddf-fc6d4de7d8c9_1712465259.6203613.png)\n![Screenshot 2024-04-07 113544.png](https://assets.leetcode.com/users/images/2b9ac218-a5a5-4f78-9850-eee0d30d266a_1712470010.8941288.png)\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n```C++ []\nclass Solution {\npublic:\n    string getSmallestString(string s, int k) {\n        int n = s.size();\n        string t = s;\n        for(int i = 0; i < n; i++){\n            int c = s.at(i)- \'a\';\n            for(int j = 0; j < 26; j++){\n                int d = min(abs(c - j), 26 - abs(c - j));\n                if(d <= k){\n                    t.at(i) = char(j + \'a\');\n                    k -= d;\n                    break;\n                }\n            }\n        }\n        return t;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def getSmallestString(self, s: str, k: int) -> str:\n        n = len(s)\n        result = list(s)\n        for i in range(n):\n            for j in range(26):\n                new_char = chr(ord(\'a\') + j)\n                diff = min((ord(new_char) - ord(s[i])) % 26 , (ord(s[i]) - ord(new_char)) % 26)\n                if diff <= k:\n                    result[i] = new_char\n                    k -= diff\n                    break\n        return "".join(result)\n```\n```Java []\nclass Solution {\n    public String getSmallestString(String s, int k) {\n        StringBuilder sb = new StringBuilder("");\n        int n = s.length();\n        for(int i = 0; i < n; i++){\n            char c = s.charAt(i);\n            if(c == \'a\')\n                sb.append(\'a\');\n            else{\n                int min = Math.min(c - \'a\', (\'z\' - c) + 1);\n                if(min <= k){\n                    k -= min;\n                    sb.append(\'a\');\n                }\n                else{\n                    int y = ((c - \'a\') - k);\n                    sb.append((char)(y + \'a\'));\n                    k = 0;\n                }\n            }\n        }\n        return sb.toString();\n    }\n}\n```\n```Python []\nclass Solution(object):\n    def getSmallestString(self, s, k):\n        """\n        :type s: str\n        :type k: int\n        :rtype: str\n        """\n        ts = [ord(x) - ord(\'a\') for x in s]\n        pre = 0\n        ans = \'\'\n        for i, x in enumerate(ts):\n            down = x\n            up = 26 - x\n            cost = min(up, down)\n            if cost <= k:\n                ans += \'a\'\n                k -= cost\n            else:\n                ans += chr(x - k + ord(\'a\'))\n                return ans + s[i + 1 : ]\n        return ans\n```\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(n)\n# Code\n```\nclass Solution:\n    def getSmallestString(self, s: str, k: int) -> str:\n        n = len(s)\n        result = list(s)\n        for i in range(n):\n            for j in range(26):\n                new_char = chr(ord(\'a\') + j)\n                diff = min((ord(new_char) - ord(s[i])) % 26 , (ord(s[i]) - ord(new_char)) % 26)\n                if diff <= k:\n                    result[i] = new_char\n                    k -= diff\n                    break\n        return "".join(result)\n```\n# Please upvote if it\'s useful ...\n![th.jpeg](https://assets.leetcode.com/users/images/c9a1d786-6ef0-43be-92f5-5229c001322f_1712470097.0999455.jpeg)\n
['Python', 'C++', 'Java', 'Python3']