Leetcode 1782 Solution

This article provides solution to leetcode question 1782 (smallest-string-with-a-given-numeric-value)

https://leetcode.com/problems/smallest-string-with-a-given-numeric-value

Solution

class Solution:
    def getSmallestString(self, n: int, k: int) -> str:
        cnt_a = (26 * n - k) // 25
        cnt_z = (k - cnt_a) // 26

        if k - cnt_a - cnt_z * 26 > 0:
            mid_ch = chr(k - cnt_a - cnt_z * 26 - 1 + ord('a'))
        else:
            mid_ch = ''

        return 'a' * cnt_a + mid_ch + 'z' * cnt_z