Ctrl K

Chop and Append

Hard
A scrolling display shows the word word. Every second two things must happen: the first step characters drop off the left end, and you append exactly step lowercase letters of your choice on the right. They do not have to be the letters that dropped off. Find the smallest positive number of seconds after which the display can show the original word again.
Example 1
word='abcabcab', step=3 → 1

After one second the part still on the tape is "abcab", which is exactly how the word begins. Appending "cab" brings back "abcabcab".

Example 2
word='banana', step=2 → 3

After the first second "nana" remains, after the second "na"; neither is how "banana" begins. By the third second nothing of the original is left, so the whole word can be written anew.

Example 3
word='abab', step=1 → 2

After one second "bab" is left, which does not fit. After two, "ab" is left: it starts "abab", so we append "ab".

Constraints
  • 1 ≤ word.length ≤ 10⁶
  • 1 ≤ step ≤ word.length
  • word consists of lowercase English letters only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPalindrome by Adding Letters in Front HardStart That Matches the End HardSuffixes Echoing the Start HardPeel Off a Repeated Start HardWord Near an Anchor Hard
loading Python…
Input
word='abcabcab', step=3
Expected
1

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