Problem

Lexicographically Smallest After One Substring Rotation

Learn this problem
Amazon logoAmazonFULLTIMEOA
See Amazon hiring insights

Problem 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) → String

Examples

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

  • 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"abac"
checking account