Easy thought process || Easy to understand O(n) Solution || Brute Force
Intuition\n Describe your first thoughts on how to solve this problem. \nCheck for the first occurence of \'a\'.\n\n# Approach\n Describe your approach to solvi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCheck for the first occurence of \'a\'.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Check for the index where \'a\' has occured the very first time.\n2. If it is index 0, then check for the very first index at which s[i] != \'a\', store this index in temp1 variable, then from temp1 check for the nearest occurence of \'a\' and store in temp2 variable, and from temp1 till temp2 - 1, replace all the letters with their previous one (alphabetically).\n3. If index of first occurence of \'a\' is greater than 0 and store it in temp variable, then simply starting from 0 and ranging to temp - 1, replace all the letters with their previous one (alphabetically).\n\n# Complexity\n- Time complexity: **O(n)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nIn worst case, we need to traverse the whole string.\n\n- Space complexity: **O(1)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nConstant extra space.\n\n# Code\n```\nclass Solution {\npublic:\n    string smallestString(string s) {\n        int temp = -1;\n        int cnt = 0;\n        for(int i = 0; i < s.size(); i++)\n        {\n            if(s[i] == \'a\')\n            {\n                cnt++;\n            }\n        }\n        if(cnt == s.size())\n        {\n            s[s.size() - 1] = \'z\';\n            return s;\n        }\n        else\n        {\n            for(int i = 0; i < s.size(); i++)\n            {\n                if(s[i] == \'a\')\n                {\n                    temp = i;\n                    break;\n                }\n            }\n\n            if(temp == 0)\n            {\n                int temp1 = 1, temp2 = 1;\n                for(int i = 1 ; i < s.size(); i++)\n                {\n                    if(s[i] != \'a\')\n                    {\n                        temp1 = i;\n                        break;\n                    }\n                }\n                for(int i = temp1; i < s.size(); i++)\n                {\n                    if(s[i] == \'a\')\n                    {\n                        temp2 = i;\n                        break;\n                    }\n                }\n\n                if(temp2 > 1)\n                {\n                    for(int i = temp1; i < temp2; i++)\n                    {\n                        s[i] = s[i] - 1;\n                    }\n                }\n                else if(temp2 == 1)\n                {\n                    for(int i = temp1; i < s.size(); i++)\n                    {\n                        s[i] = s[i] - 1;\n                    }\n                }\n\n                return s;\n            }\n            else if(temp == -1)\n            {\n                for(int i = 0; i < s.size(); i++)\n                {\n                    s[i] = s[i] - 1;\n                }\n                return s;\n            }\n            else\n            {\n                for(int i = 0; i < temp; i++)\n                {\n                    s[i] = s[i] - 1;\n                }\n                return s;\n            }\n        }\n    }\n};\n```
['String', 'Python', 'C++', 'Java']