Ctrl K

Front Letter to the Back

Hard
You are given a string word of lowercase English letters and an integer reach. In one move you may take any one of the first reach letters, cut it out and append it to the end of the string. You may make as many moves as you like, including none. Return the lexicographically smallest string you can end up with.
Example 1
word="dbca", reach=1 → 'adbc'

Only the first letter can move, which gives the rotations dbca, bcad, cadb and adbc. The smallest is adbc.

Example 2
word="dbca", reach=2 → 'abcd'

Choosing between the first two letters lets you reach any arrangement, including the letters in sorted order.

Example 3
word="zz", reach=1 → 'zz'
Constraints
  • 1 ≤ reach ≤ len(word) ≤ 1000
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClosest Clock Readings MediumRound Prices to a Total MediumPlus-Minus Calculator With Brackets HardAmount in Words HardSigns Between the Digits Hard
loading Python…
Input
word="dbca", reach=1
Expected
'adbc'

Run checks the first 3 tests in your browser. Submit checks all 119 tests on the server, hidden ones included.