Katkat

Find the Smallest String After One Decrement

Point72Machine Learning Engineer · Technical Screen

Last updated

This question evaluates string manipulation, lexicographic ordering, and reasoning about local character transformations including wrap-around behavior.

You are given a string s consisting only of lowercase English letters.

You must perform exactly one operation:

  1. Choose one non-empty contiguous substring of s.
  2. Replace every character in that substring with the previous character in the alphabet.
  • For example, b becomes a, c becomes b, and so on.
  • The character a wraps around and becomes z.

Return the lexicographically smallest string that can be obtained after exactly one operation.

Example:

Input: s = "cbabc"
Output: "baabc"

Explanation: Choose the substring "cb" and decrement it to "ba", producing "baabc".