3106. Lexicographically Smallest String After Operations With Constraint
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 62% Topics: String, Greedy
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# greedy
class Solution(object):
def getSmallestString(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
result = map(lambda x: ord(x)-ord('a'), s)
for i in xrange(len(result)):
d = min(result[i]-0, 26-result[i])
result[i] = 0 if d <= k else result[i]-k
k -= min(d, k)
if k == 0:
break
return "".join(map(lambda x: chr(x+ord('a')), result))
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions