Beats 90+ % cpu time - C++ soln using loops sraightforward, commented
Approach\nCheck for leading \'a\'s in the string s.\n\nFrom here onwards, only work with the substring to the RIGHT of any leading \'a\'s (I call this the strin
# Approach\nCheck for leading \'a\'s in the string s.\n\nFrom here onwards, only work with the substring to the RIGHT of any leading \'a\'s (I call this the string rhs).  Note rhs may = s if there are no leading \'a\'s\n\nFind first \'a\' in substring rhs.\nTurn \'back\' every char in rhs from index [0] to [index of last non-a in rhs]  As soon as you modify any character, set the flag `noChangesYet` to false.  Initialize this flag to true at the top.\n\nIf by now, `noChangesYet` is still true, then your string `s` was such that it ends in one or more `a` characters and these are the only a characters in the string `s`.  Either way - just make the very last character in the string `s` into a `z` and return this as as your solution.\n\nElse (`noChangesYet` is false by now), return concatenated `lhs+rhs` as the solution.\n\n# Complexity\n- Time complexity:\nO(n) for a few searches and iteration through substrings to dial chars back by 1.\n# Code\n```\nclass Solution {\npublic:\n    string smallestString(string s) {\n        bool noChangesYet = true;\n        size_t firstNonA = 0;\n        for(firstNonA=0; s[firstNonA]==\'a\'&&firstNonA<s.size(); firstNonA++) ; //firstNonA now has the first non-a index in the string, this is the start of the rollback\n        string lhs;\n        string rhs;\n        if(firstNonA != s.size()){\n            //some leading \'a\'s found, split the string:\n            lhs = s.substr(0,firstNonA); //may be empty\n            rhs = s.substr(firstNonA,std::string::npos);//everthing from first non \'a\' char onwards\n        }\n        else{//no \'a\' chars found\n            lhs = "";\n            rhs = s;\n        }\n        size_t firsta = rhs.find(\'a\');\n        if(firsta == std::string::npos)\n            firsta=rhs.size();\n        for(int i=0; i<firsta; i++){\n            rhs[i]=rhs[i]-1;\n            noChangesYet = false;\n        }\n        if(noChangesYet){\n            s[s.size()-1] = \'z\';\n            return s;\n        }\n        return lhs+rhs;\n    }\n};\n\n```
['C++']