Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using string matching strategy.
You are given a 0-indexed string word and an integer k.
At every second, you must perform the following operations:
k characters of word.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.
Example 1:
Input: word = "abacaba", k = 3 Output: 2 Explanation: At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac". At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state. It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
Example 2:
Input: word = "abacaba", k = 4 Output: 1 Explanation: At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state. It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
Example 3:
Input: word = "abcbabcd", k = 2 Output: 4 Explanation: At every second, we will remove the first 2 characters of word, and add the same characters to the end of word. After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state. It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
Constraints:
1 <= word.length <= 50 1 <= k <= word.lengthword consists only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: String Matching
"abacaba" 3
"abacaba" 4
"abcbabcd" 2
longest-happy-prefix)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3029: Minimum Time to Revert Word to Initial State I
class Solution {
public int minimumTimeToInitialState(String word, int k) {
int n = word.length();
for (int i = k; i < n; i += k) {
if (word.substring(i).equals(word.substring(0, n - i))) {
return i / k;
}
}
return (n + k - 1) / k;
}
}
// Accepted solution for LeetCode #3029: Minimum Time to Revert Word to Initial State I
func minimumTimeToInitialState(word string, k int) int {
n := len(word)
for i := k; i < n; i += k {
if word[i:] == word[:n-i] {
return i / k
}
}
return (n + k - 1) / k
}
# Accepted solution for LeetCode #3029: Minimum Time to Revert Word to Initial State I
class Solution:
def minimumTimeToInitialState(self, word: str, k: int) -> int:
n = len(word)
for i in range(k, n, k):
if word[i:] == word[:-i]:
return i // k
return (n + k - 1) // k
// Accepted solution for LeetCode #3029: Minimum Time to Revert Word to Initial State I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3029: Minimum Time to Revert Word to Initial State I
// class Solution {
// public int minimumTimeToInitialState(String word, int k) {
// int n = word.length();
// for (int i = k; i < n; i += k) {
// if (word.substring(i).equals(word.substring(0, n - i))) {
// return i / k;
// }
// }
// return (n + k - 1) / k;
// }
// }
// Accepted solution for LeetCode #3029: Minimum Time to Revert Word to Initial State I
function minimumTimeToInitialState(word: string, k: number): number {
const n = word.length;
for (let i = k; i < n; i += k) {
if (word.slice(i) === word.slice(0, -i)) {
return Math.floor(i / k);
}
}
return Math.floor((n + k - 1) / k);
}
Use this to step through a reusable interview workflow for this problem.
At each of the n starting positions in the text, compare up to m characters with the pattern. If a mismatch occurs, shift by one and restart. Worst case (e.g., searching "aab" in "aaaa...a") checks m characters at nearly every position: O(n × m).
KMP and Z-algorithm preprocess the pattern in O(m) to build a failure/Z-array, then scan the text in O(n) — never backtracking. Total: O(n + m). Rabin-Karp uses rolling hashes for O(n + m) expected time. All beat the O(n × m) brute force of checking every position.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.