#3029
Medium Algorithms Minimum time to revert word to initial state i
String Rolling Hash String Matching Hash Function
42.3% acceptance
Feb 25, 2026
176
34
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.
Return the minimum time greater than zero required for word to revert to its initial state.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn minimum_time_to_initial_state(word: String, k: i32) -> i32 {
let n = word.len();
let k = k as usize;
let w = word.as_bytes();
for t in 1.. {
let skip = t * k;
if skip >= n { return t as i32; }
if w[skip..] == w[..n-skip] { return t as i32; }
}
unreachable!()
}
}