✅Beats 100.00% 🔥 || C++ || Java || C# || Easy Explanation || Beginner Friendly🔥🔥🔥
\n# Intuition\nIntuition:\nThe intuition behind this solution is to construct the smallest string lexicographically by replacing each non-\'a\' character with t
![image.png](https://assets.leetcode.com/users/images/f6f28d1c-acc2-4b70-b70f-67ad3efb2946_1712725949.106869.png)\n# Intuition\nIntuition:\nThe intuition behind this solution is to construct the smallest string lexicographically by replacing each non-\'a\' character with the character one step before it in the alphabetical order. If there are consecutive \'a\' characters, they are kept as-is in the resulting string. If all characters after a certain point are \'a\', the entire remaining substring is appended to the result.\n\n# Approach\n- Initialize an empty string **result** to store the constructed smallest string.\n- Initialize a variable count to keep track of the number of \'a\' characters encountered so far.\n- Iterate through the input string s character by character.\n- For each character **s[i]**: \n    - If **s[i]** is not \'a\', append **(char)(s[i] - 1)** to the **result** string. \n    - If **s[i]** is \'**a**\', append \'**a**\' to the **result** string. \n    - Increment the **count** variable if **s[i]** is \'**a**\'. \n    - If **s[i]** is \'**a**\' and **count** is not equal to **result.size()**, it means that all the remaining characters in s are \'a\' or does not need any manipulation. In this case, append the remaining substring **s.substr(i + 1, s.size() - i)** to the **result** string and return it.\n- After the loop completes, if count is equal to **result.size()**, it means that all characters in s were \'**a**\'. In this case, replace the last character of **result** with \'**z**\'.\n- Return the **result** string.\n\n# Complexity\n- Time complexity: **O(n)**\nWhere n is the length of the input string s. This is because we iterate through the string once, performing constant-time operations at each character.\n\n- Space complexity: **O(n)**\nWhere **n** is the length of the input string **s**. This is because, in the worst case, we need to construct a new string result with the same length as s when all characters are non-\'a\' characters.\n\n```C++ []\nclass Solution {\npublic:\n    string smallestString(string s) {\n        string result;\n        int count = 0;\n\n        for (int i = 0; i < s.size(); i++) {\n            // If the current character is \'a\', increment the counter\n            count += s[i] == \'a\' ? 1 : 0;\n            \n            // If the current character is not \'a\', append the character one step before it\n            // If the current character is \'a\', append \'a\'\n            result += (s[i] != \'a\' ? (char)(s[i] - 1): \'a\');\n            //Found the substing, append the remaining substring and return the result\n            if(s[i] == \'a\' && count != result.size())\n               return result += s.substr(i + 1, s.size() - i);\n        }\n        //All characters in the input string were \'a\'\n        if(count == result.size())\n            result[result.size() - 1] = \'z\';\n        return result;\n    }\n};\n```\n```Java []\nclass Solution {\n    public String smallestString(String s) {\n        var result = new StringBuilder();\n        var count = 0;\n\n        for (var i = 0; i < s.length(); i++) {\n\n            count += s.charAt(i) == \'a\' ? 1 : 0;\n            result.append(s.charAt(i) != \'a\' ? (char)(s.charAt(i) - 1) : \'a\');\n            if(s.charAt(i) == \'a\' && count != result.length())\n                return result.append(s.substring(i+1)).toString();\n        }\n\n        if(count == result.length())\n           result.setCharAt(result.length() - 1, \'z\');\n        return result.toString();\n    }\n}\n```\n```C# []\nusing System.Text;\n\npublic class Solution {\n    public string SmallestString(string s) {\n        \n        var result = new StringBuilder();\n        var aCount = 0;\n\n        for (var i = 0; i < s.Length; i++) {\n\n            aCount += s[i] == \'a\' ? 1 : 0;\n            result.Append(s[i] != \'a\' ? (char)(s[i] - 1): \'a\');\n            if(s[i] == \'a\' && aCount != result.Length)\n                return result.Append(s.AsSpan(i + 1, s.Length - i - 1)).ToString();\n        }\n\n        if(aCount == result.Length)\n            result[^1] = \'z\';\n        return result.ToString();\n    }\n}\n```\n
['String', 'Greedy', 'C++', 'Java', 'C#']