Skip to content

Commit 07bbbf1

Browse files
authored
Create smallest-string-with-a-given-numeric-value.py
1 parent 5db1ad2 commit 07bbbf1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def getSmallestString(self, n, k):
6+
"""
7+
:type n: int
8+
:type k: int
9+
:rtype: str
10+
"""
11+
MAX_DIFF = ord('z')-ord('a')
12+
13+
k -= n
14+
result = ['a']*n
15+
for i in reversed(xrange(n)):
16+
tmp = min(k, MAX_DIFF)
17+
result[i] = chr(ord('a')+tmp)
18+
k -= tmp
19+
if k == 0:
20+
break
21+
return "".join(result)

0 commit comments

Comments
 (0)