Beats 100% | Easy to understand
Intuition\n Describe your first thoughts on how to solve this problem. \n- a as prefix is already small - no need to change them\n- replacing characters in the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- `a` as prefix is already small - no need to change them\n- replacing characters in the front the string makes more lexigraphically smaller\n- Note that we can only change one substring. So we will try to make this substring as lengthy as possible.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- ingnore all `a` in the prefix\n- replace with preceding character until the next `a` is found\n- if the string contains all `a`, we have to make a change, so change the last `a` to `z`\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```java []\nclass Solution {\n    public String smallestString(String s) {\n        int n = s.length();\n        StringBuilder str = new StringBuilder(s);\n\n        int i = 0;\n\n        //ignore all \'a\' in the beginning\n        while(i < n && str.charAt(i) == \'a\'){\n            i++;\n        } \n\n        //reduce all chars until next \'a\'\n        for(int j = i; j < n; j++) {\n            char c = s.charAt(j);\n            if(c == \'a\') {\n                break;\n            } else {\n                str.setCharAt(j, (char)(c - 1));\n            }\n        }\n\n        //all a\'s - then change last char only\n        if(i == n) {\n            str.setCharAt(n - 1, \'z\');\n        }\n        return str.toString();\n        \n    }\n}\n```
['Java']