FastPrepLexicographically Smallest String After Substring Operation

Lexicographically Smallest String After Substring Operation

Point72 logoPoint72● MediumFULLTIMEOA

Updated

Problem statement

You are given a string s of lowercase English letters.

Choose one non-empty substring of s. A substring is a run of letters that sit next to each other. Replace every letter in the substring with the letter just before it in the alphabet. So b becomes a, c becomes b, and a becomes z. You must do this exactly once.

Return the lexicographically smallest string you can get. A string is smaller when it comes first in dictionary order.

Function

smallestString(s: String) → String

Examples

Example 1

s = "cbabc"return = "baabc"

Pick the first two letters, cb. Lower them: c becomes b and b becomes a. The string becomes baabc. Picking the a in the middle would turn it into z, which makes the string bigger. No other pick gives a smaller string, so the answer is baabc.

Constraints

  • 1 <= s.length <= 100000
  • s contains only lowercase English letters (a to z)

More Point72 interview questions

Similar questions at other companies

All Point72 interview questionsSee Point72 hiring insights
Saved
class Solution {
    public String smallestString(String s) {
        // write your code here
    }

}
Checking account…