Ctrl K

Word on a Conveyor

Medium
You are given a string text of lowercase English letters and an integer step. Every second the string must change in two moves: 1. the first step characters are erased; 2. any step characters of your choice are appended to the end (they do not have to be the ones that were erased). Both moves happen every second; you cannot skip a turn. Return the smallest positive number of seconds after which the string can be equal to the original text again.
Example 1
text="banana", step=2 → 3

After one second "nana" remains plus two free letters, but "nana" is not the start "bana". After two seconds it is "na" against "ba", still no. After three seconds nothing of the original is left, so all six letters can be written back.

Example 2
text="abcab", step=3 → 1

Removing "abc" leaves "ab", which is exactly how the word starts. Appending "cab" brings back "abcab" after one second.

Example 3
text="zzz", step=1 → 1

Any tail made of z matches the start of the word, so one second is enough.

Constraints
  • 1 ≤ len(text) ≤ 50
  • 1 ≤ step ≤ len(text)
  • text contains only lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAnchor Near a Companion MediumTarget From Word Beginnings MediumPalindrome by Adding Letters in Front HardStart That Matches the End HardSuffixes Echoing the Start Hard
loading Python…
Input
text="banana", step=2
Expected
3

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