Minimum Time to Revert Word to Initial State I - 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 = "abacaba", k = 3
›
Output:
2
💡 Note:
At t=1: remove "aba", remaining = "caba". "caba" is not a prefix of "abacaba". At t=2: remove "abacab", remaining = "a". "a" is a prefix of "abacaba". Return 2.
Example 2 — Immediate Match
$
Input:
word = "abcabc", k = 2
›
Output:
1
💡 Note:
At t=1: remove "ab", remaining = "cabc". Need to check if we can form "abcabc" by adding characters. Since "cabc" doesn't match prefix, we continue checking until we find a match.
Example 3 — Complete Removal
$
Input:
word = "abc", k = 1
›
Output:
3
💡 Note:
At t=1: remove "a", remaining = "bc". At t=2: remove "ab", remaining = "c". At t=3: remove "abc", remaining = "". Empty string can always be reconstructed to original.
Constraints
- 1 ≤ word.length ≤ 50
- 1 ≤ k ≤ word.length
- word consists only of lowercase English letters.
Visualization
Tap to expand
Understanding the Visualization
1
Input
Original word and removal count k
2
Process
Remove k characters, check if suffix can form original
3
Output
Minimum time when word can revert
Key Takeaway
🎯 Key Insight: The word reverts when its remaining suffix matches a prefix of the original word
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code