Minimum Time to Revert Word to Initial State II - Problem
Transform and Restore: You're given a string word and an integer k. Every second, you must perform two operations: remove the first k characters from the word, then add any k characters to the end. The key insight is that you can choose what characters to add - they don't have to be the same ones you removed!

Your goal is to find the minimum time greater than zero required to make the word identical to its original state. This is essentially asking: what's the smallest number of operations needed before we can restore the word by choosing the right characters to append?

Example: If word = "abcdef" and k = 2, after removing "ab" we have "cdef". We need to add 2 characters to make it potentially restorable to "abcdef".

Input & Output

basic_example.py โ€” Python
$ Input: word = "abacaba", k = 3
โ€บ Output: 2
๐Ÿ’ก Note: After 1 operation: remove 'aba' โ†’ remaining 'caba'. Since 'abacaba' doesn't start with 'caba', we can't restore yet. After 2 operations: remove 'cab' โ†’ remaining 'a'. Since 'abacaba' starts with 'a', we can restore by adding 'bacaba' to get 'abacaba'. Answer: 2.
simple_case.py โ€” Python
$ Input: word = "abcdef", k = 2
โ€บ Output: 3
๐Ÿ’ก Note: After 1 operation: 'cdef' remains. 'abcdef' doesn't start with 'cdef'. After 2 operations: 'ef' remains. 'abcdef' doesn't start with 'ef'. After 3 operations: empty string remains. We can always restore from empty, so answer is 3.
repeating_pattern.py โ€” Python
$ Input: word = "abababab", k = 2
โ€บ Output: 1
๐Ÿ’ก Note: After 1 operation: remove 'ab' โ†’ remaining 'ababab'. Since 'abababab' starts with 'ababab', we can restore by adding 'ab' at the end. Answer: 1.

Visualization

Tap to expand
Word Restoration Processword = 'abacaba', k = 3Time 0abacabaTime 1cabaTime 2aRestore!Answer: 2 operationsโœ— No matchโœ“ Prefix match
Understanding the Visualization
1
Initial Setup
Start with word 'abacaba' and k=3. We need to find when remaining suffix matches word prefix.
2
First Operation
Remove 'aba' โ†’ remaining 'caba'. Check: does 'abacaba' start with 'caba'? No.
3
Second Operation
Remove 'cab' โ†’ remaining 'a'. Check: does 'abacaba' start with 'a'? Yes!
4
Restoration
Since suffix 'a' matches prefix 'a', we can add 'bacaba' to restore the original word.
Key Takeaway
๐ŸŽฏ Key Insight: Use the Z-algorithm to precompute suffix-prefix matches in O(n) time, then check only positions that are multiples of k for optimal efficiency.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass to compute Z-array, then O(n/k) checks in O(1) each

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space for Z-array or hash values

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค word.length โ‰ค 106
  • 1 โ‰ค k โ‰ค word.length
  • word consists only of lowercase English letters
  • Time limit: Must handle large inputs efficiently
Asked in
Google 25 Microsoft 18 Amazon 12 Meta 8
41.0K Views
Medium Frequency
~25 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen