Achieving the Lexicographically Smallest String via Alphabetical Substitution Decrementation
Intuition\nThe task is to make the string as small as possible by transforming any substring\'s characters to their preceding character in the alphabet. The app
# Intuition\nThe task is to make the string as small as possible by transforming any substring\'s characters to their preceding character in the alphabet. The approach is to target the earliest possible non-\'a\' character since converting any character to \'a\' yields the smallest lexicographical value. However, since \'a\' turns into \'z\', a careful approach is needed to avoid unintended increases in string value.\n\n# Approach\n1. **Scan for the First Transformable Character:** Start scanning the string from the left for the first character that is not \'a\'.\n2. **Transform the Character:** Once a non-\'a\' character is found, change it to its preceding alphabet character. If the character is \'a\', skip it to avoid turning it into \'z\', which would increase the string\'s value.\n3. **Continue Transformation:** Continue transforming subsequent characters in the substring to ensure that the resulting substring is as small as possible lexicographically.\n4. **Handle All \'a\' Strings:** If the string consists entirely of \'a\'s, the last character should be converted to \'z\' to meet the condition of modifying at least one character.\n\n# Complexity\n- **Time complexity:** $$O(n)$$, where $$n$$ is the length of the string. The process involves a single traversal of the string to make the necessary transformations.\n- **Space complexity:** $$O(1)$$, since the operation modifies the string in place and does not require additional space proportional to the input size.\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string smallestString(string s) {\n        bool changeMade = false;\n        int n = s.length();\n        int i = 0;\n        while (i < n && s[i] == \'a\') {\n            i++;\n        }\n        while(i < n && s[i]!=\'a\'){\n            s[i] -= 1;\n            changeMade = true;\n            i++;\n        }\n        if(!changeMade){\n            s[n-1] = \'z\';\n        }\n        return s;\n    }\n};\n```\n## Did you like the solution?\nIf you found this solution helpful, please consider giving it a vote up! Your support is greatly appreciated and helps others find this solution too. \uD83D\uDE4F\uD83C\uDFFB
['C++']