---
# System prepended metadata

title: 2734. Lexicographically Smallest String After Substring Operation
tags: [Leetcode, Medium, Greedy]

---

Link: https://leetcode.com/problems/lexicographically-smallest-string-after-substring-operation/description/
## 思路
先跳过所有开头的a
如果这个string全都是a 那么只能对最后一个字符实施operation
然后把下一个a之前的所有字符都replace
## Code
```java=
class Solution {
    public String smallestString(String s) {
        int i = 0, n = s.length();
        char[] A = s.toCharArray();
        while(i<n && A[i]=='a'){
            i++;
        }
        if(i==n) A[n-1]='z';
        while(i<n && A[i]!='a'){
            --A[i];
            i++;
        }
        return String.valueOf(A);
    }
}
```