✅ C++ Solution with explanation | O(n)
Explanation\n\n1. Initialise a string ans all with \'a\'s (to ensure that the answer will be lexicographically small\n2. Start traversing the string from the en
**Explanation**\n```\n1. Initialise a string ans all with \'a\'s (to ensure that the answer will be lexicographically small\n2. Start traversing the string from the end, and if k >= 25 we add \'z\' instead of \'a\' else we add \n\'b\' or \'c\' or .... \'y\' depending on k. (ans[i] += min(25, k)).\n3. We decrease the value of k by the replaced element. (k -= min(25, k)).\n4. If k < 0 we break the loop.\n5. Finally, we return the updated string ans.\n```\n**Code**\n```\nclass Solution {\npublic:\n    string getSmallestString(int n, int k) {\n        string ans = "";\n\t\t//Initialise string ans with \'a\'s\n        for (int i = 0 ; i < n ; i++){\n            ans += "a";\n        }\n\t\t// The value of \'a\' is 1 so we decrease k by n\n        k -= n;\n\t\t//Index j for the last position\n        int j = n - 1;\n        while (k > 0){\n\t\t\t//Update the rightmost caracter by min(25, k)\n\t\t\t//It means : ans[j] = min(\'a\' + 25, \'a\' + k);\n            ans[j] += min(25, k);\n\t\t\t//Decrease K & j\n            k -= min(25, k);\n            j--;\n        }\n\t\t//Return the answer\n        return ans;\n    }\n};\n```\n# Please Upvote if that helps you
[]