Python 3 | 5-line Greedy O(N) | Explanation
Understand the question\n- Question is asking for smallest string, that being said, \n\t- we want to have as most a on the left side as possible\n\t- If using a
### Understand the question\n- Question is asking for *smallest string*, that being said, \n\t- we want to have as most *a* on the left side as possible\n\t- If using *a* doesn\'t match given number, we can try some letter with larger number \n\t- *z* will be the last letter we want to use, so we want to have them all the way to the right\n- The more *z* we use on the right, the less *a* we need on the left\n- For example, `n=2, k=27`\n\t- best case (smallest) `az` = 1 + 26\n\t- `za` is worst, since we put *z* on the left\n\t- `aa` is smaller than `az` but it\'s not a valid solution\n\t- `by` is a valid solution, but it\'s larger than `az` since we didn\'t start from *a*\n### Explanation\n- So the idea is\n\t- First, make sure string have enough length `n` by initializing string with all letters as *a* of length `n`\n\t- Adjust the string so that it can fully cover the given number `k`\n\t\t- We do this by using *z* from the right side\n\t- Ultimately, there **might** have some left over that using *z* will cause a overflow (`k` becomes negative)\n\t\t- then we use corresponding letter for it\n\t- for example, `n=3, k=29`\n\t\t- start with `aaa`, now `k` is 26\n\t\t- start putting `z` from the right, we can get `aaz`, now `k` is 1\n\t\t- since `z` will overflow 1, we use `b` instead, to **just** cover k\n\t\t- final result will be `abz`\n- `ans`: final answer in list format; `z` number of *z* I need, `nz` the letter **just** to cover the left over\n### Implementation\n```\nclass Solution:\n    def getSmallestString(self, n: int, k: int) -> str:\n        ans = [\'a\'] * n\n        k, i = k-n, n-1\n        z, nz = divmod(k, 25)                # `z`: number of *z* I need, `nz`: ascii of the letter just to cover the leftover\n        ans[n-1-z] = chr(nz + ord(\'a\'))      # adjust the left over `k` using mod\n        return \'\'.join(ans[:n-z]) + \'z\' * z  # make final string & append `z` to the end\n```
['Greedy', 'Python', 'Python3']