908. Rewind the Word

Hard7.5 points estimatedStringString Matching4 hints

A scrolling shop sign shows the lowercase text word. Every second the sign drops its first k letters from the left, and the electrician may then append any k letters of their choice at the right end (the letters can differ from the ones that were dropped). If fewer than k letters are left, the sign simply drops them all and the new text consists only of the appended letters.

Return the smallest positive number of seconds after which the sign can show exactly word again. A value of k larger than the length of word is allowed.

Aim for O(n) time and O(n) space, where n is the length of word.

Example 1

Input:
word = "quokkaquokkaquo", k = 5
Output:
3
Explanation:

With k = 5, the earliest second at which the word can be restored is 3.

Example 2

Input:
word = "tiktoktiktokti", k = 4
Output:
3
Explanation:

With k = 4, the earliest second at which the word can be restored is 3.

Constraints

1 ≤ word.length ≤ 105

1 ≤ k ≤ 105

word consists of lowercase English letters.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…