✅ C++ || Dry Run || Easy || Simple & Short || O(N)
1663. Smallest String With A Given Numeric Value\nKNOCKCAT\n\n\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Lexi
# 1663. Smallest String With A Given Numeric Value\n**KNOCKCAT**\n\n```\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Lexicographically smallest string of length n and sum k with Dry Run.\n5. Please Upvote if it helps\u2B06\uFE0F\n```\n``` ```\n[LeetCode](http://github.com/knockcat/Leetcode) **LINK TO LEETCODE REPOSITORY**\n``` ```\n\n\nPlease upvote my comment so that i get to win the 2022 giveaway and motivate to make such discussion post.\n**Happy new Year 2023 to all of you**\n**keep solving keep improving**\nLink To comment\n[Leetcode Give away comment](https://leetcode.com/discuss/general-discussion/2946993/2022-Annual-Badge-and-the-Giveaway/1734919)\n\n**ALGORITHM**\n* **Initialise an string** of length n **with all \'a\'s**.\n* Start **traversing from the end of the string** and **add the element of the string by \'z\'** if **k >= 25. as a is already inserted with ascii 1** , adding 25 to 1 **result in 26 i.e \'z\'.**\n* At the **same time decrease the value of K** **by replaced element value i.e. for a = 1, b = 2, c = 3, \u2026, z = 26.**\n* Check for **K < 0** condition and **break the for loop.**\n* **Return the new string formed.**\n``` ```\n\n**DRY RUN**\n![image](https://assets.leetcode.com/users/images/272aa8e3-adf7-4bdb-bb78-dc392ebfc684_1647911019.839921.jpeg)\n\n``` ```\n**CODE WITH EXPLANATION**\n```\n\t\t\t\t\t// \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09Please upvote if it helps \uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\uD83D\uDE09\nclass Solution {\npublic:\n    string getSmallestString(int n, int k) {\n        \n        // initialising string of length n with all \'a\';\n        string str(n,\'a\');\n        \n        // as all \'a\' are 1 and therefore we have subtract there sum;\n        k -= n;\n        \n        while( k > 0)\n        {\n            // turning rightmost digit , \'a\' into \'z\' (\'a\' + 25, or \'a\' + k)\n            // while k is positive.\n            str[--n] += min(25,k);\n            k -= min(25,k);\n        }\n        \n        return str;\n    }\n\t// for github repository link go to my profile.\n};\n```\n
['String', 'C']