Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 1.16 KB

_3106. Lexicographically Smallest String After Operations With Constraint.md

File metadata and controls

48 lines (34 loc) · 1.16 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 29, 2024

Last updated : June 29, 2024


Related Topics : String, Greedy

Acceptance Rate : 63.309 %


Solutions

Python

class Solution:
    def getSmallestString(self, s: str, k: int) -> str:
        output = []

        for i, c in enumerate(s) :
            if not k :
                output.append(s[i:])
                break

            distToA = min(ord(c) - ord('a'), 26 - (ord(c) - ord('a')))
            if k >= distToA :
                k -= distToA
                output.append('a')
                continue

            if distToA > 0 and k :
                output.append(chr(ord(c) - k))
                k = 0

        return ''.join(output)