O(n) C++ solution with explanation and dry run
We have to find the smallest lexiographical string with size of n and numeric value of k.\n\nFor a string to be  lexiographically small, it\'s beginning charact
We have to find the smallest lexiographical string with size of n and numeric value of k.\n\n**For a string to be  lexiographically small, it\'s beginning characters must be smallest.**\n\nWe initialise a string as **ans** with size n, having each character as **\'a\'**. \n\nLet\'s take an example for the better understanding:\n\nn=5, k(numeric value)=73\n\n**ans = "aaaaa", k=68**, this is the result after initialization. Now if the value of k would have been 0 that means we already got our answer. There are no invalid test cases(n=3, k=2) here, so we do not need to worry about the cases resulting in our code failure.\n\n**"aaaaa" is the smallest string of size 5 that exists.**\n\nWe will update the value of ans in reverse order, because the beginning elements must be smallest to get the smallest  lexiographical string.\n\nIf k>25, that means we can place \'z\' here because the numeric value required is must greater, and we update the k as k-25, we keep on doing it until it reaches the condition of k<25.\nThe reason we are subtracting 25 not 26 is because we already considered the numeric value of a in one of the initial steps where we wrote n=n-k.\n\nelse(k<=25), that means this is the last update required\n\n\n\nLet me dry run it for the better understanding.\n\n\n**After Initial Steps(For n=5, k=73)**\nans = "aaaaa", k=68\n\n**Iteration of loop**\n**i=4**\nBefore Update : ans="aaaaa", k=68\nAfter Update : ans="aaaaz", k=43\n\n**i=3**\nBefore Update : ans="aaaaz", k=43\nAfter Update : ans="aaazz", k=18\n\n**i=2**\nBefore Update :  ans="aaazz", k=18\nAfter Update : ans="aarzz", k=0, **break;**\n\n**Note**\n\nFor those who might have trouble understanding the meaning of **char ch = char(96+k+1)**\nAscii value of a lowercase letter starts with 97, if k=18 like in above dry run for i=2\n\n\'a\' was already the initial value, therefore we have added 96 not 97.\nk is added because it is the numeric value(meaning, we are calcualting the the no. of letters ahead of \'a\').\n1 is added because it is 1-indexed, meaning, the numeric value of a was 1 not 0.\n\n```\nclass Solution {\npublic:\n    string getSmallestString(int n, int k) {\n        string ans = "";\n        \n        for(int i=0 ; i<n ; i++)\n            ans += \'a\';\n        \n        k=k-n;\n        \n        if(k==0)\n            return ans;\n        \n        for(int i=n-1 ; i>=0 ; i--){\n            if(k>25){\n                k=k-25;\n                ans[i]=\'z\';\n            }\n            else{\n                char ch = char(96+k+1);\n                ans[i] = ch;\n                break;\n            }\n        }\n        \n        return ans;\n    }\n};\n```
[]