mediumArraysPattern: String Manipulation
Minimized Rotation String Solution
Problem Statement
Given a non-empty string `source` and an integer `rotations`, find the lexicographically smallest string that can be obtained by performing `rotations` number of rotations on the `source` string. A single rotation involves moving the last character of the string to the front.
Examples
Example 1:
Input:undefined
Output:efabcd
Explanation: The string 'abcdef' rotated 2 times results in 'efabcd' because the last two characters 'ef' are moved to the front.
Example 2:
Input:undefined
Output:xyz
Explanation: No rotation occurs when k is 0.
Constraints
- 1 ≤ length of string ≤ 10^5
- 1 ≤ k ≤ 10^6
- String contains only lowercase English letters
Time: O(n) Space: O(n)
The optimized approach involves concatenating the input string with itself and finding the smallest substring of the same length as the input string, resulting in a time complexity of O(n).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
