File tree 1 file changed +11
-10
lines changed
scripts/algorithms/S/Smallest String With A Given Numeric Value
1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 1870 ms (Top 22.43%) | Memory: 15.5 MB (Top 41.47%)
1
2
class Solution :
2
3
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
8
9
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
You can’t perform that action at this time.
0 commit comments