BackmediumArrays Uber

Cyclic String Rotation Solution

Problem Statement

Given a string and an integer k, determine the lexicographically smallest string that can be obtained by rotating the input string k times.

Example 1
Input
abcdeab 3
Output
cdeab

Explanation: Step-by-step: Given the input string 'abcdeab' and the rotation count 3, we first find the minimum substring of the input string, which is 'ab'. Then, we rotate the input string 3 times to match this minimum substring. The resulting string is 'cdeab', which is the lexicographically smallest string that can be obtained by rotating the input string 3 times.

Example 2
Input
cdefab 5
Output
cdefab

Explanation: Step-by-step: Given the input string 'cdefab' and the rotation count 5, we first find the minimum substring of the input string, which is 'cdefab'. Then, we rotate the input string 5 times to match this minimum substring. The resulting string is 'cdefab', which is the lexicographically smallest string that can be obtained by rotating the input string 5 times.

Constraints

  • 1 ≤ length of string ≤ 10^5
  • 1 ≤ k ≤ 10^6
  • String contains only lowercase English letters
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free