Skip to content

Latest commit

 

History

History
52 lines (39 loc) · 1.3 KB

_2734. Lexicographically Smallest String After Substring Operation.md

File metadata and controls

52 lines (39 loc) · 1.3 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 : 32.34 %


Solutions

Python

class Solution:
    def smallestString(self, s: str) -> str:
        substringStarted  = False
        substringModified = False
        output = []

        for i, c in enumerate(s) :
            if substringStarted :
                if c == 'a' :
                    output.append(s[i:])
                    break
                else :
                    output.append(chr(ord(c) - 1))
            elif c != 'a' :
                substringModified = True
                substringStarted = True
                output.append(chr(ord(c) - 1))
            else :
                output.append(c)

        if not substringModified :
            output[-1] = 'z'

        return ''.join(output)