Java O(n) simple easy code
Intuition\nWhen we modify any character by previous character it always result in a smaller string lexicographically unless that character is an \'a\'.\nTransof
# Intuition\nWhen we modify any character by previous character it always result in a smaller string lexicographically unless that character is an \'a\'.\nTransofrming \'a\' to \'z\' will lead to lexicographically bigger string.\n\n# Approach\nWe need to track position of first non \'a\' character and start transforming from there and keep on doing it until we approach an \'a\' character. If there are all \'a\' in string then we will just modify last \'a\' to \'z\' and rest of the strign will be same.\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: no extra space\n\n# Code\n```java []\nclass Solution {\n    public String smallestString(String s) {\n        int i=0;\n        StringBuilder sb = new StringBuilder();\n        for(; i<s.length(); i++){\n            // continue the for loop until character is \'a\'\n            char ch = s.charAt(i);\n            if(ch == \'a\'){\n                continue;\n            } else {\n                break;\n            }\n        }\n        // if we reach the end and string is having all \'a\' in it then we need at least one last \'a\' to be converted to \'z\'\n        if(i == s.length()){\n            sb.append(s.substring(0, s.length()-1));\n            sb.append(\'z\');\n        } else {\n            // Now we reached at first non \'a\' character. We will copy initial substring as it is and start doing operation of subtracting 1 from next chars until we approach an \'a\'\n            sb.append(s.substring(0, i));\n            while(i<s.length() && s.charAt(i) != \'a\'){\n                char ch2 = (char) (s.charAt(i) - 1);\n                sb.append(ch2);\n                i++;\n            }\n            // Once we approach and \'a\', we will copy the rest of the string as it is.\n            sb.append(s.substring(i, s.length()));\n        }\n\n        return sb.toString();\n\n\n    }\n}\n```
['Java']