#1663
Smallest String With A Given Numeric Value
pupil · 595 · lc medium +29 · verified · 67.4% accepted · 1,922 likes · top 74%
Description
Each lowercase letter has a numeric value equal to its 1-based position in the alphabet (a=1, b=2, ..., z=26). The numeric value of a string is the sum of its characters' values. Given integers n and k, return the lexicographically smallest string of length n with numeric value equal to k.
Example 1:
Input: n = 3, k = 27
Output: "aay"
Explanation: The numeric value of the string is 1 + 1 + 25 = 27, and it is the smallest string with such a value and length equal to 3.
Example 2:
Input: n = 5, k = 73
Output: "aaszz"
Code
1
2
3