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:
3
💡 Note:
At t=1: remove "ab", remaining = "cabc". "cabc" is not a prefix of "abcabc". At t=2: remove "abca", remaining = "bc". "bc" is not a prefix of "abcabc". At t=3: remove "abcabc", remaining = "". Empty string can always be reconstructed to original. Return 3.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code