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
sis non-empty.- The operation must be performed exactly once. Choosing a substring of length
1leaves the string unchanged.
More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Maximum Non-Adjacent House ValueONSITE INTERVIEW · Seen Jun 2026
- Running Delivery Time MediansONSITE INTERVIEW · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
public String smallestStringAfterOneRotation(String s) {
// write your code here
}s"baca"
expected"abca"
sign in to submit