My first instinct was to just decrement the whole string, which is obviously wrong because 'a' wraps to 'z' and makes things worse.
Recognize that to minimize the string lexicographically, we should decrement the longest prefix of non-'a' characters, stopping at the first 'a' (since decrementing 'a' would make it 'z', which is worse). If the entire string consists of 'a's, decrement the last character to avoid making the string worse. Implement this by scanning the string, applying the decrement to the chosen substring, and returning the result.
Pro tip: Clarify edge cases upfront, especially strings with all 'a's or multiple 'a's, and mention that the operation must be applied exactly once. This shows attention to detail and avoids off-by-one errors.
We need the lexicographically smallest string after decrementing exactly one contiguous substring. Lexicographic order means earlier characters dominate, so we want to make the earliest possible characters smaller.
Decrementing a character reduces its value unless it's 'a', which becomes 'z' (worse). So we should decrement a prefix of characters that are not 'a', starting from the first character, and stop at the first 'a'.
If the string consists entirely of 'a's, any decrement will turn an 'a' into 'z', making the string larger. To minimize the damage, decrement only the last character, changing it to 'z'.
Scan the string, apply the decrement to the chosen substring, and return the result. Test with cases like 'abc', 'aaa', 'aab', and 'zzz' to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.