Problem
Lexicographically Smallest After One Substring Rotation
Learn this problemProblem statement
You are given a string s. You must choose one non-empty contiguous substring of s and rotate that substring to the right by one position exactly once.
Rotating a substring to the right by one position moves its last character to the front of that substring, while every other character in the substring shifts one position to the right.
Return the lexicographically smallest string that can be obtained after performing the operation.
Function
smallestStringAfterOneRotation(s: String) → StringExamples
Example 1
s = "baca"return = "abac"Choose the whole string baca. Rotating it right by one position moves the last character to the front, giving abac, which is the lexicographically smallest result obtainable.
Example 2
s = "cba"return = "acb"Choose the whole string "cba". Rotating it right gives "acb", which is the smallest possible result.
Constraints
sis non-empty.- The operation must be performed exactly once. Choosing a substring of length
1leaves the string unchanged.