Lexicographically Smallest String After Substring Operation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
    def smallestString(self, s: str) -> str:
        i = 0
        while i < len(s) and s[i] == 'a':
            i += 1
        if i >= len(s):
            s = s[:len(s)-1] + 'z'
            return s
        while i < len(s) and s[i] != 'a':
            s = s[:i] + chr(ord(s[i]) - 1) + s[i+1:]
            i += 1
        return s