3029. Minimum Time to Revert Word to Initial State I
Description
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.
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 <= 501 <= k <= word.lengthwordconsists only of lowercase English letters.
Solutions
Solution 1: Enumeration
Thinking
Each operation drops the first \(k\) characters and appends arbitrary ones. The word returns to its start iff the leftover suffix equals the equally long prefix. \(n \le 50\) allows enumerating the number of operations.
After \(i\) operations the leftover is \(\textit{word}[ik:]\), which must equal \(\textit{word}[:n-ik]\). If it never matches, \(\lceil n/k \rceil\) operations empty the string.
We try \(k,2k,\ldots\) with direct string comparison and otherwise return the ceiling.
Let's assume that if we can restore word to its initial state with only one operation, it means that word[k:] is a prefix of word, i.e., word[k:] == word[:n-k].
If there are multiple operations, let's assume \(i\) is the number of operations, then it means that word[k*i:] is a prefix of word, i.e., word[k*i:] == word[:n-k*i].
Therefore, we can enumerate the number of operations and check whether word[k*i:] is a prefix of word. If it is, then return \(i\).
The time complexity is \(O(n^2)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of word.
1 2 3 4 5 6 7 | |
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 | |
Solution 2: Enumeration + String Hash
Thinking
Part I compares \(O(n)\)-length strings and costs \(O(n^2)\). That passes here, but equality can be preprocessed to \(O(1)\).
String hashing fingerprints every substring, so a prefix check is a single query and the whole scan is linear.
Based on Solution 1, we can also use string hashing to determine whether two strings are equal.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) is the length of word.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |