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 k characters of word.
  • Add any k characters to the end of word.

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
Minimum Time to Revert Word: "abcab" with k=2Original:"abcab"t=1 Remove 2:"cab"Cannot restoret=2 Remove 4:"b"Cannot restoret=3 Remove all:""Can restore!Answer: 3
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
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~35 min Avg. Time
234 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