Lexicographically Smallest After One Substring Rotation
Updated
Problem statement
You are given a string s. Choose one non-empty substring of s, which is a run of characters that sit next to each other. Rotate that substring to the right by one position. You must do this exactly once.
Rotating a substring to the right by one position moves its last character to the front of that substring. Every other character in the substring shifts one position to the right.
Return the lexicographically smallest string you can get after the rotation. The lexicographically smallest string is the one that comes first when you compare two strings character by character: at the first position where they differ, the string with the smaller character code comes first, so every uppercase letter comes before every lowercase letter.
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. No other choice gives a string that comes earlier in dictionary order, so this is the answer.
Example 2
s = "cba"return = "acb"Choose the whole string "cba". Rotating it right gives "acb". No other choice gives a string that comes earlier in dictionary order, so this is the answer.
Constraints
sis non-empty.- The operation must be performed exactly once. Choosing a substring of length
1leaves the string unchanged.
More Amazon Online Assessment questions
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Get Smallest Base SegmentOA · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026