3031. Minimum Time to Revert Word to Initial State II Hard
1/**
2 * [3031] Minimum Time to Revert Word to Initial State II
3 *
4 * You are given a 0-indexed string word and an integer k.
5 * At every second, you must perform the following operations:
6 *
7 * Remove the first k characters of word.
8 * Add any k characters to the end of word.
9 *
10 * Note that you do not necessarily need to add the same characters that you removed. However, you must perform both operations at every second.
11 * Return the minimum time greater than zero required for word to revert to its initial state.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: word = "abacaba", k = 3
16 * Output: 2
17 * 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".
18 * 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.
19 * It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state.
20 *
21 * <strong class="example">Example 2:
22 *
23 * Input: word = "abacaba", k = 4
24 * Output: 1
25 * 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.
26 * It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state.
27 *
28 * <strong class="example">Example 3:
29 *
30 * Input: word = "abcbabcd", k = 2
31 * Output: 4
32 * Explanation: At every second, we will remove the first 2 characters of word, and add the same characters to the end of word.
33 * After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state.
34 * It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state.
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= word.length <= 10^6
40 * 1 <= k <= word.length
41 * word consists only of lowercase English letters.
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-ii/
47// discuss: https://leetcode.com/problems/minimum-time-to-revert-word-to-initial-state-ii/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn minimum_time_to_initial_state(word: String, k: i32) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_3031() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.