Minimum Time to Revert Word to Initial State II - Problem
You are given a 0-indexed string word and an integer k.
At every second, you must perform the following operations:
- Remove the first
kcharacters ofword. - Add any
kcharacters to the end ofword.
Note that you do not necessarily need to add the same characters that you removed. However, you must perform both operations at every second.
Return the minimum time greater than zero required for word to revert to its initial state.
Input & Output
Example 1 — Basic Case
$
Input:
word = "abcab", k = 2
›
Output:
3
💡 Note:
At t=1: remove "ab" → "cab", can't restore. At t=2: remove "abca" → "b", can't restore. At t=3: remove all → "", can add "abcab" to restore original.
Example 2 — Early Return
$
Input:
word = "abab", k = 2
›
Output:
2
💡 Note:
At t=1: remove "ab" → "ab", can add "ab" to get "abab". But this doesn't work. At t=2: remove "abab" → "", can restore by adding "abab".
Example 3 — Single Character
$
Input:
word = "a", k = 1
›
Output:
1
💡 Note:
At t=1: remove "a" → "", can add "a" to restore original word.
Constraints
- 1 ≤ word.length ≤ 106
- 1 ≤ k ≤ word.length
- word consists only of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original word and removal size k
2
Process
Remove k chars each second, check if restorable
3
Output
Minimum time to restore original word
Key Takeaway
🎯 Key Insight: Find the smallest time where the remaining suffix can be extended to match the original word
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code