Minimum Time to Revert Word to Initial State II - Problem
Transform and Restore: You're given a string
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 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
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
โ Linear Growth
Space Complexity
O(n)
Space for Z-array or hash values
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code