Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 546 Bytes

1663.md

File metadata and controls

24 lines (22 loc) · 546 Bytes

1663. Smallest String With A Given Numeric Value

Solution 1 (time O(n), space O(1))

# Greedy
class Solution(object):
    def getSmallestString(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: str
        """
        ans = ""
        for i in range(n):
            t = k - (n - 1 - i) * 26
            if t <= 0:
                ans += "a"
                k -= 1
            else:
                ans += chr(ord('a') + t - 1)
                k -= t
        return ans