✅Very easy & Simple Approach💯||🔥2 Examples✅|| Just rephrase the Problem ||✅Compare distance to 'a'
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem at first may look critical. But it is not. We are given an integer \'k\' th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem at first may look critical. But it is not. We are given an integer \'k\' that is the ***"maximum"*** distance between answer and original string \'s\'. You have to return a string that is lexicographically smallest.\n\nIt can also be rephrased to, generating string closest to "aaa..."(length same as \'s\'), with a distance of \'k\' or less.\n\nYou can also think of it as we can transform any character to its immediate neighbour a total of \'k\' or less times to make string \'s\' closest to "aaa..."(length same as \'s\').\n\nThe preference is given to left most char, as it it most significant in determining the lexicographical order.\n\nSo we start operating from left to right. Also dont forget about the cyclic thing. We have a,b,c... after we reach z.\n\n---\n\n# Examples\n\n##### 1. For a string `s= "ybdc"` and `k=7`\n\n- At first we can change `0th char \'y\'` to its next neighbour 2 times to make it \'a\'. We use 2 opeations. And the operations left is (k-2) = (7-2) = 5. (The distance of current string from original string is 2.) Current string is `"abdc"`\n    1. y->z\n    2. z->a\n- Next we change `1st char \'b\'` to its previous neighbour 1 time to make it \'a\'. We use 1 opeation. And the operations left is (k-2-1) = (7-2-1) = 4. (The distance of current string from original string is 3.) Current string is `"aadc"`\n    1. b->a\n\n- Next we change `2nd char \'d\'` to its previous neighbour 3 time to make it \'a\'. We use 3 opeation. And the operations left is (k-2-1-3) = (7-2-1-3) = 1. (The distance of current string from original string is 6.) Current string is `"aaac"`\n    1. d->c\n    2. c->b\n    3. b->a\n- Next we change `3rd char \'c\'` to its previous neighbour 1 time to make it \'b\'. As we are only left with 1 operation. We use 1 opeation. And the operations left is (k-2-1-3-1) = (7-2-1-3-1) = 0. (The distance of current string from original string is 7.) Current string is `"aaab"`\n    1. c->b\n- We have no more oprations left. Or, we have reached the maximum distance. So, we return the answer.\n\n---\n\n##### 2. For a string `s= "ybdc"` and `k=77`\n\n- At first we can change `0th char \'y\'` to its next neighbour 2 times to make it \'a\'. We use 2 opeations. And the operations left is (k-2) = (77-2) = 75. (The distance of current string from original string is 2.) Current string is `"abdc"`\n    1. y->z\n    2. z->a\n- Next we change `1st char \'b\'` to its previous neighbour 1 time to make it \'a\'. We use 1 opeation. And the operations left is (k-2-1) = (77-2-1) = 74. (The distance of current string from original string is 3.) Current string is `"aadc"`\n    1. b->a\n\n- Next we change `2nd char \'d\'` to its previous neighbour 3 time to make it \'a\'. We use 3 opeation. And the operations left is (k-2-1-3) = (77-2-1-3) = 71. (The distance of current string from original string is 6.) Current string is `"aaac"`\n    1. d->c\n    2. c->b\n    3. b->a\n- Next we change `3rd char \'c\'` to its previous neighbour 2 time to make it \'a\'. We use 2 opeation. And the operations left is (k-2-1-3-1) = (77-2-1-3-2) = 69. (The distance of current string from original string is 8.) Current string is `"aaaa"`\n    1. c->b\n    2. b->a\n\n- We have no characters left. This is lexicographically smallest string. So, we return the answer.\n\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- In any iteration, We have to make the ith char closest to a. So, we first calculate to distance of ith char to \'a\' in a cyclic manner. We take the minimum the distance of both by moving forward and by moving backward. \n\n- In any case if \'a\' is not reachable with no of opeations left (`distanceToa>k`). We simply change the character to its previous character k times.\n\n- Otherwise, we make the `s[i]=\'a\'`, use `distanceToa` operations, decreasing the no of operations by distanceToa.\n\n---\n\n# Complexity\n- Time complexity: ***O(n)***\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: ***O(1)***\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n---\n# Code\n```\nclass Solution {\npublic:\n    string getSmallestString(string s, int k) {\n        for(int i=0; i<s.size(); i++){\n            if(k==0)return s;\n            int distanceToa = min(s[i]-\'a\', \'z\'+1-s[i]);\n            if(distanceToa<=k){\n                s[i]=\'a\';\n                k-=distanceToa;\n            }else{\n                s[i]=s[i]-k;\n                k=0;\n            }\n        }\n        return s;\n    }\n};\n```\n---\n\n## HOPE MY ANSWER HELPS. IF IT HELPED, UPVOTE PLEASE.\n\n![upvote if it helped](https://assets.leetcode.com/users/images/a275a449-c2db-4195-9ee5-f15743a62510_1712467436.386334.jpeg)\n\n
['C++']