Problem

Lexicographically Smallest After One Substring Rotation

AmazonFULLTIMEOA
See Amazon hiring insights

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.

Examples
01 · Example 1
s = "baca"
return = "abca"

Choose substring "ba". Rotating it right gives "ab", so the whole string becomes "abca".

02 · Example 2
s = "cba"
return = "acb"

Choose the whole string "cba". Rotating it right gives "acb", which is the smallest possible result.

Constraints
  • s is non-empty.
  • The operation must be performed exactly once. Choosing a substring of length 1 leaves the string unchanged.
More Amazon problems
drafts saved locally
public String smallestStringAfterOneRotation(String s) {
  // write your code here
}
s"baca"
expected"abca"
sign in to submit