Skip to content

Commit 94ba0d7

Browse files
committed
Runtime: 1870 ms (Top 22.43%) | Memory: 15.5 MB (Top 41.47%)
1 parent 90f8153 commit 94ba0d7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
# Runtime: 1870 ms (Top 22.43%) | Memory: 15.5 MB (Top 41.47%)
12
class Solution:
23
def getSmallestString(self, n: int, k: int) -> str:
3-
ans = ['a']*n # Initialize the answer to be 'aaa'.. length n
4-
val = n #Value would be length as all are 'a'
5-
6-
for i in range(n-1, -1, -1):
7-
if val == k: # if value has reached k, we have created our lexicographically smallest string
4+
ans = ['a']*n # Initialize the answer to be 'aaa'.. length n
5+
val = n #Value would be length as all are 'a'
6+
7+
for i in range(n-1, -1, -1):
8+
if val == k: # if value has reached k, we have created our lexicographically smallest string
89
break
9-
val -= 1 # reduce value by one as we are removing 'a' and replacing by a suitable character
10-
ans[i] = chr(96 + min(k - val, 26)) # replace with a character which is k - value or 'z'
11-
val += ord(ans[i]) - 96 # add the value of newly appended character to value
12-
13-
return ''.join(ans) # return the ans string in the by concatenating the list
10+
val -= 1 # reduce value by one as we are removing 'a' and replacing by a suitable character
11+
ans[i] = chr(96 + min(k - val, 26)) # replace with a character which is k - value or 'z'
12+
val += ord(ans[i]) - 96 # add the value of newly appended character to value
13+
14+
return ''.join(ans) # return the ans string in the by concatenating the list

0 commit comments

Comments
 (0)