Problems > Lexicographically Smallest Rotation

Lexicographically Smallest Rotation

Difficulty: 1400 2000 ms 128 MB

strings

Among all |S| rotations of S, print the lexicographically smallest one.

Input: one line with the string S.

Output: the smallest rotation.

Constraints:

  • 1 ≤ |S| ≤ 2000
  • lowercase letters

Sample Tests

Input 1
cba
Output 1
acb
Input 2
baca
Output 2
abac

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.