[Java/C++/Python] with Easy Explanation ✅
Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n--
# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n- Upvote if you like my solution\n\n# Java Code\n\n```\npublic class Solution {\n    public String getSmallestString(String s) {\n        int i = 0, n = s.length();\n        \n        // Find the first non-\'a\' character\n        while (i < n && s.charAt(i) == \'a\') {\n            i++;\n        }\n        \n        if (i == n) {\n            // If the string is all \'a\'s, change the last character to \'z\'\n            char[] chars = s.toCharArray();\n            chars[n - 1] = \'z\';\n            return String.valueOf(chars);\n        }\n        \n        int j = i;\n        char[] chars = s.toCharArray();\n        \n        // Decrement the characters until the first non-\'a\' character\n        while (j < n && chars[j] != \'a\') {\n            chars[j] -= 1;\n            j++;\n        }\n        \n        return String.valueOf(chars);\n    }\n}\n\n```\n# Cpp Code\n```\nclass Solution {\npublic:\n    string getSmallestString(string s) {\n        int i = 0, j = 0;\n        int n = s.size();\n\n        // Find the first non-\'a\' character\n        while (i < n && s[i] == \'a\') {\n            i++;\n        }\n\n        if (i == n) {\n            // If the string is all \'a\'s, change the last character to \'z\'\n            s[n - 1] = \'z\';\n            return s;\n        }\n\n        // Decrement the characters until the first non-\'a\' character\n        while (i < n && s[i] != \'a\') {\n            s[i] -= 1;\n            i++;\n        }\n\n        return s;\n    }\n};\n\n```\n# Phython Code\n```\nclass Solution:\n    def smallestString(self, s: str) -> str:\n        i = 0\n        n = len(s)\n        \n        # Find the first non-\'a\' character\n        while i < n and s[i] == \'a\':\n            i += 1\n        \n        if i == n:\n            # If the string is all \'a\'s, change the last character to \'z\'\n            chars = list(s)\n            chars[n - 1] = \'z\'\n            return \'\'.join(chars)\n        \n        j = i\n        chars = list(s)\n        \n        # Decrement the characters until the first non-\'a\' character\n        while j < n and chars[j] != \'a\':\n            chars[j] = chr(ord(chars[j]) - 1)\n            j += 1\n        \n        return \'\'.join(chars)\n\n```\n
['Two Pointers', 'String', 'Python', 'C++', 'Java']